How to get the position of the caret of before a change occurs in the editor?

SyntaxEditor for Windows Forms Forum

Posted 9 months ago by John
Version: 22.1.1
Avatar

I need to be able to get the position of the caret before ANY change occurs in the editor. This could be a regular ITextChange event or in could be undo/redo event. Or it could be something I didn't list because I didn't know about it.

Currently, this is the best I can come up and it works for text changes but NOT undo/redos

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    // Capture the caret position before a change occurs
    PreChangeCaretPosition = editor.ActiveView.Selection.CaretPosition;
}

 private bool IsCaretAtBeginningOfLine => PreChangeCaretPosition.Character == 0;

Comments (6)

Answer - Posted 9 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

You could probably handle the SyntaxEditor.ViewSelectionChanged event and cache e.CaretPosition's value.


Actipro Software Support

Posted 9 months ago by John
Avatar

That seems to fire after the PreviewKeyDown event does. I need to know if the caret is at the beginning of the line BEFORE any change occurs in the editor.

Posted 9 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Correct, it fires after the selection is changed.  I should have clarified more, but I'm suggesting that you cache the TextPosition it returns.  Then when the next text change occurs, you can refer to the cached TextPosition to see where the caret was.  That should reliably give you the previous caret position, regardless of how a text change occurs.


Actipro Software Support

Posted 9 months ago by John
Avatar

Ok that makes sense now. Is there any event I could use that fires before The DocumentTextChanged event does? Like in the scenario where you split a line in the middle by pressing the enter key, the caret was initially not at the beggining of the line but after the text change it is at the beginning of the line.

SelectionChanged fires after the DocumentTextChanged event

Posted 9 months ago by John
Avatar

Nevermind, it was an application specific place I needed to capture the caret position (with respect to getting the caret before an undo/redo occurs). Thanks anyway!

Posted 9 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Sometimes the selection is changed programmatically prior to text changes being applied right after.  What was previously suggested would handle that scenario and should be reliable.

Another possible option is the ITextChange is usually assigned a PreSelectionPositionRanges value when one can be assigned.  That stores the selection state (a caret is at the end of a selection) prior to the text change being applied.  We then use that when undo-ing to restore the selection.  The Primary property on the ITextPositionRangeCollection instance that could be there would be the "main" selection, with the primary caret being at its "end".  There can be more than one position range in the ITextPositionRangeCollection when there are multiple selections.


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.