Numeric TextBox

后端 未结 13 1095
再見小時候
再見小時候 2020-12-03 20:01

Im new to programming and I dont know very much about but I\'m making a calculator, and i want to use a textbox that only acepts numbers and decimals, and when the user past

13条回答
  •  北海茫月
    2020-12-03 20:19

    Add an event handler for the textbox you want to be numeric only, and add the following code:

    private void textBoxNumbersOnly_KeyPress(object sender, KeyPressEventArgs e)
    {
       if (Char.IsDigit(e.KeyChar) || e.KeyChar == '\b')
       {
          e.Handled = false;
       }
       else
       {
          e.Handled = true;
       }
    }
    

    This allows for numbers 0 to 9, and also backspaces (useful IMHO). Allow through the '.' character if you want to support decimals

提交回复
热议问题