How do I make a textbox that only accepts numbers?

后端 未结 30 1685
梦如初夏
梦如初夏 2020-11-21 06:05

I have a windows forms app with a textbox control that I want to only accept integer values. In the past I\'ve done this kind of validation by overloading the KeyPress event

30条回答
  •  無奈伤痛
    2020-11-21 06:49

    private void txt3_KeyPress(object sender, KeyPressEventArgs e)
    {
        for (int h = 58; h <= 127; h++)
        {
            if (e.KeyChar == h)             //58 to 127 is alphabets tat will be         blocked
            {
                e.Handled = true;
            }
        }
        for(int k=32;k<=47;k++)
        {
            if (e.KeyChar == k)              //32 to 47 are special characters tat will 
            {                                  be blocked
                e.Handled = true;
            }
        }
    }
    

    try this is very simple

提交回复
热议问题