ScintillaNET icon indicating copy to clipboard operation
ScintillaNET copied to clipboard

How to get/set the Caret Position?

Open Naicos opened this issue 7 years ago • 7 comments

I'm working at a XML Editor with Search and GoToLine. Most parts are already implemented but i cannot find a way to set the Caret position for both.

Naicos avatar Mar 29 '18 11:03 Naicos

Use property CurrentPosition for getting the current position. And call method GotoPosition to set the Caret to a specific a position.

Aghlara avatar Mar 29 '18 11:03 Aghlara

With GoToPosition(LineNumber) the Caret gets set only to the character of the given number (For example if my text has 106 Lines and I want to do to Line 50 im in Line 2 and character 50)

Naicos avatar Mar 29 '18 12:03 Naicos

Sorry I don't understand you. Method GotoPosition accepts the character position not a line number.

Aghlara avatar Mar 29 '18 12:03 Aghlara

Yes but I want to set the Caret to a specific Line with my own GoToLine Dialog

Naicos avatar Mar 29 '18 12:03 Naicos

You can look at Lines property which each of them has the Position and then use GoToPosition.

Aghlara avatar Mar 29 '18 12:03 Aghlara

I thought:

MyScintilla.Lines[23].Goto();

or

MyScintilla.GotoPosition(MyScintilla.Lines[23].Position);

Stumpii avatar Mar 30 '18 04:03 Stumpii

@Stumpii and @Aghlara are correct. If you want to get to a specific character on a specific line, do the math that would determine what position that is, e.g. line position start + offset.

However, I might suggest that you're making it unnecessarily hard for yourself. Why isn't your search/find dialog giving you the document position directly instead of the line+offset? If it did that you could pass the value straight to GoToPosition.

jacobslusser avatar Mar 30 '18 20:03 jacobslusser