Typing in the Textbox

Asked

Viewed 193 times

0

I’m doing a project in the visual studio and I need help. I wonder if there is a way or how to make it so that when I type in the textbox,this I typed start at the front regardless of the position of the cursor or that it was sort of excluding until I got to the front. A textbox would be phone kind of like this:(__)____-____.

  • You want to wear a mask in the field that’s it?

1 answer

3


You can use the event Enter of MaskedTextBox that will be triggered whenever the user clicks on the component and inside it uses the method Select, in this way:

In the method below, it is also validated if the Textbox is empty to place the cursor at the beginning.

To use this way, you need to change the property TextMaskFormat for ExcludePromptAndLiterals. - This will make the property Text of TextBox contains only the text typed 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)));
    }
}
  • Do I have to add some reference? Because some words haven’t changed the color.

  • No. What is that "did not change the color"?

  • Isnullorwhitespace, Text, Begininvoke, Select.

  • And it didn’t work.

  • Are you sure you didn’t write something wrong?

  • I did, it was my mistake, vlw.

  • Right, you can mark the answer as correct if it helped you by clicking on below the arrows to vote =).

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.