Search code examples
vb.netmouse-position

Mouse position at end of textbox on Mouse Click event


I have a textbox that when the user clicks on it the cursor position should always be at the end of the text.

So far I have the following code under the textbox_mouseClick event

Private Sub RTextBox_MouseClick(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles RTextBox.MouseClick
    RTextBox.SelectionStart = Len(RTextBox.Text)
End Sub

This works but first the mouse position jumps to where ever the person clicked and then its jumps to the end.

Can I make the mouse position stay at the end and not move when they click on the textbox?


Solution

  • Use theMouseDown Event instead.

    MouseDown - occurs when a mouse button is pressed

    MouseClick - occurs when a mouse is pressed and released

    So in your situation the mouse is being pressed, moving to the middle of textbox, and only then reaching the code that tells it to remain at the end.

    If you use MouseDown it will intercept and remain at the end before it moves.

    (@γηράσκω δ' αεί πολλά διδασκόμε answer)