-2

I have read a lot of code examples in this subject but it doesnt work. It should be no problem but it failed in my project where in other people project it work. I have text box in C# that should be contains only numbers. In my project if i have a text box with text :

-----------------------
|         32          |
|                     |
----------------------|

and i add a char " T" my resualt is :

-----------------------
|         T3          |
|                     |
----------------------|

It remove the first char instead the last char My code :

private void txt_NumOfIkun_KeyPress(object sender, KeyPressEventArgs e)
    {
        char charAdded = e.KeyChar;

        if (txt_NumOfIkun.TextLength > 1)
                txt_NumOfIkun.Text = txt_NumOfIkun.Text.Substring(0, txt_NumOfIkun.TextLength - 1);
            else
                txt_NumOfIkun.Text= "";
    }
Ben Tubul
  • 45
  • 1
  • 9

1 Answers1

0
if (e.KeyChar < '0' || e.KeyChar > '9')
{
     e.Handled = true;
}
craig1231
  • 3,769
  • 4
  • 31
  • 34