Best way to limit textbox decimal input in c#

后端 未结 4 1848
南笙
南笙 2020-12-16 07:49

How can I make a textbox in which can be only typed a number like 12.00 or 1231231.00 or 123123

I\'ve done this in a very long way and I\'m looking for the best and

4条回答
  •  情话喂你
    2020-12-16 08:34

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsNumber(e.KeyChar))
            {
                e.Handled = !(((TextBox)sender).SelectionStart != 0 && (e.KeyChar.ToString() == Application.CurrentCulture.NumberFormat.NumberDecimalSeparator && ((TextBox)sender).Text.IndexOf(Application.CurrentCulture.NumberFormat.NumberDecimalSeparator) == -1));
            }
        }
    

    And you should check onLeave for Length == 0 I think...

提交回复
热议问题