Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
355
Tracking Cursor Position in UltraTxtEditor
posted

Hi,

I'm trying to track the cursor position in an UltraTextEditor. Reading the SelectionStart property works but I want to update my postion label when the mouse is clicked in the editor  and when the arrow keys (left, right) are pressed.

I tried the MouseClick event but it never occurs and MouseDown only occurs once.

For the keys I used the KeyPress event but it didn't come up for the arrow keys (only for characters, numbers, ...). Trying the KeyDown event works but the position is not correct.

Here is what I have now:

Private Sub TxtTblContent_KeyDown(...) Handles TxtTblContent.KeyDown

If (e.KeyCode = Keys.Right) OrElse (e.KeyCode = Keys.Left) Then

LblCursorPosition.Text = TxtTblContent.SelectionStart

End If

End Sub

Private Sub TxtTblContent_MouseDown(...) Handles TxtTblContent.MouseDown

If e.Button = Windows.Forms.MouseButtons.Left Then

LblCursorPosition.Text = TxtTblContent.SelectionStart

End If

End Sub

 

And what I tried:

Private Sub TxtTblContent_KeyPress(...) Handles TxtTblContent.KeyPress

If (e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Right)) OrElse (e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Left)) Then

LblCursorPosition.Text = TxtTblContent.SelectionStart

End If

End Sub

The MouseClick event was implemented like the MouseDown event.

 

Any suggestions?

Thanks,

KurtJulius

  • 69832
    Suggested Answer
    Offline posted

    UltraTextEditor uses a TextBox control when it is in edit mode. The TextBox class does not expose any events that fire when the text selection changes. The only way I can think of to receive a notification in realtime when the text selection is to implement IMessageFilter on the Application and handle the EM_SETSEL windows message for the UltraTextEditor's TextBox. You can get a reference to the TextBox by handling UltraTextEditor's ControlAdded event.