I have six .NET 2.0 MaskedTextBoxes in a user control (With a mask on each
of 3 numeric characters). When the length of the text in the right-most
masked textbox is 0, I want to put the cursor at the end of the previous
(5th) masked textbox. I've tried the following:
Private Sub mtb6_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles mtb6.TextChanged
Select Case Me.mtb6.Text.Length
Case Me.intMaxEntryLength
Exit Select
Case 0
Me.mtb5.SelectionStart = Me.mtb5.Text.Length
Me.mtb5.Focus()
Case Else
Exit Select
End Select
End Sub
All I ever get, regardless of where I set the SelectionStart property, is
the cursor showing up between the 1st and 2nd character. As I would
suspect, if I set the SelectionStart property to -1, I get an exception.
However, If I set the SelectionStart property to some absurd number (say
43465491651), I get the same results (i.e. between the 1st and 2nd
character).
Is this a problem with the control, or just my implementation of it?
Regards,
Jason.
Jason Allred - 28 Aug 2007 18:52 GMT
I found the solution in the MSDN documentation:
You can programmatically move the caret within the text box by setting the
SelectionStart to the position within the text box where you want the caret
to move to and set the SelectionLength property to a value of zero (0). The
text box must have focus in order for the caret to be moved.
So all I had to do was call the Focus() method prior to setting the
SelectionStart and SelectionLength Properties.
>I have six .NET 2.0 MaskedTextBoxes in a user control (With a mask on each
>of 3 numeric characters). When the length of the text in the right-most
[quoted text clipped - 26 lines]
>
> Jason.