textbox validation for allow one “ . ” value c#

后端 未结 8 1705
悲&欢浪女
悲&欢浪女 2021-01-23 08:56

I want textbox validation for allowing only one . value and only numbers. Means my textbox value should take only numerics and one . value. Value shoul

8条回答
  •  忘了有多久
    2021-01-23 09:48

    try this one

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar)
                   && !char.IsDigit(e.KeyChar)
                   && e.KeyChar != '.')
                e.Handled = true;
    
            // only allow one decimal point
            if (e.KeyChar == '.'
                && textBox1.Text.IndexOf('.') > -1)
                e.Handled = true;
        }
    

提交回复
热议问题