blah blah blah is here! blah blah » Close

up1down
link

Dear All,

I have a question about textbox in c sharp. I want to add a "-" after I type 2 characters inside a text box, the thing is if I use textBox1.Text += "-", the type pointer returns to the beginning. Any idea on this simple question?

Thanks in advance,




E

last answered one year ago

1 answers

link

Try this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text.Length == 2)
{
textBox1.Text += "-";
textBox1.SelectionStart = textBox1.Text.Length;
}
}

thanks foamy, it works but only for 2 first characters. Is it possible to include an - everytime the user types 2 characters?

vulpes
17279

Try changing: if (textBox1.Text.Length == 2) to: if ((textBox1.Text.Length % 3) == 2)

foamy
2499

good one vulpes :)

Feedback