How to force the cursor to the first position in a MaskedTextbox control when user clicks mouse in field



Customer has a Windows Forms application which uses a MaskedTextBox control.
When the control is entered using the Tab key there is no problem as the cursor will be in the starting position.
If a user instead clicks in the center of the control the cursor remains in the center of the control.

Customer asks: Is there a way to force the cursor to the right or left edge of a MaskedTextBox? I want the insertion point to always be on the right for numeric data and on the left for text.


You could do something like the following in the MouseUp event for the MaskedTextbox control:

       method-id maskedTextBox1_MouseUp final private.
       procedure division using by value sender as object e as type System.Windows.Forms.MouseEventArgs.

         invoke type SendKeys::Send("{HOME}")

       end method.

If your control's RightToLeft property is set to Yes then this will position the cursor to the rightmost position.

If RightToLeft is set to No then it will position the cursor on the leftmost position.

You could also use the MouseClick event and use the following:

          set maskedTextBox1::SelectionStart to 0

Comment List
Related Discussions