Selecting input masks

dpatfield66

Well-known member
Joined
Apr 6, 2006
Messages
136
Programming Experience
5-10
How can I get the entire text to be selected in a masked text box?
I have code on GotFocus() that does it for all my masked text boxes, but isn't there some kind of property I could select that would let this happen.

Right now, without my code, if a user enters a masked text box, the cursor goes to the end of the value. I'd like the cursor to highlight the entire text, so a user can override the value without having to move the mouse, or keys.
 
Hi,

Your Query
" without my code, if a user enters a masked text box, the cursor goes to the end of the value. I'd like the cursor to highlight the entire text, so a user can override the value without having to move the mouse, or keys."

In .Net 2003, you can use first TextBox.Focus() and then TextBox.SelectAll()

this will set the focus on textBox and then SelectAll wil select all the text in the text box

- Jay
 
Thanks for the reply.

Actually, I had also mentioned in my post that I have code that does the task, and wonderered if there was a way to do it without coding each GotFocus event, or LostFocus (or Leave) event.

I want this to happen with ALL my masked text boxes (all my controls, actually), so is there a property that will allow the entire text to be selected?
 
Create your own UserControl. Change Inherits from UserControl to MaskedTextBox (and remove the AutoScaleMode line that will error now). Override the OnGotFocus where you SelectAll like this:
VB.NET:
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
  MyBase.OnGotFocus(e)
  Me.SelectAll()
End Sub
Then you can add your control to form instead of the regular MaskedTextBox control. It's all the same as the regular MaskedTextBox control except your added code, in other words it will SelectAll when it get focus by Tab into it.
 
I'm not sure I follow you on this one...would I still have to code at an event (In this case, OnGotFocus)? I'm trying to set a property for each control that will allow entry to select the entire text, so I can avoid an event handler.
 
OnGotFocus is what happens internally in the control class when its GotFocus event occur.

There is no property like you ask for in the standard MaskedTextBox. You can either handle the GotFocus event for all your regular MaskedTextBox controls OR you can create your own usercontrol that adds this default behaviour as described in previous post.
 
Back
Top