You can use the Enter
event of the MaskedTextBox
to be triggered whenever the user clicks on the component and uses the Select
method as follows:
In the method below, it is also validated if the TextBox is empty to put the cursor at the beginning.
To use this way, you need to change the TextMaskFormat
property to ExcludePromptAndLiterals
. - This will cause the Text
property of the TextBox
to contain only the text entered by the user.
private void maskedTextBox1_Enter(object sender, EventArgs e)
{
var txtbox = (MaskedTextBox)sender;
if (string.IsNullOrWhiteSpace(txtbox.Text))
{
this.BeginInvoke((MethodInvoker)(() => txtbox.Select(0, 0)));
}
}