Numeric TextBox

后端 未结 13 1093
再見小時候
再見小時候 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:45

    useful for decimal numeric entry but has some bugs if (rightclick and paste) the other text. :D

     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string original = (sender as TextBox).Text;
            if (!char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            if (e.KeyChar == '.')
            {
                if (original.Contains('.'))
                    e.Handled = true;
                else if (!(original.Contains('.')))
                    e.Handled = false;
    
            }
            else if (char.IsDigit(e.KeyChar)||e.KeyChar=='\b')
            {
                e.Handled = false;
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题