How do I make a textbox that only accepts numbers?

后端 未结 30 1620
梦如初夏
梦如初夏 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:53

    You can use the TextChanged event

    private void textBox_BiggerThan_TextChanged(object sender, EventArgs e)
    {
        long a;
        if (! long.TryParse(textBox_BiggerThan.Text, out a))
        {
            // If not int clear textbox text or Undo() last operation
            textBox_LessThan.Clear();
        }
    }
    

提交回复
热议问题