0
Did you try like this:
You nned to attach an keypress event in textbox properties.
http://csharp.net-informations.com/gui/key-press-cs.htm
- private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
- {
-
- if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar))
- && (e.KeyChar != '.') && (e.KeyChar != '-'))
- e.Handled = true;
-
-
- if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
- e.Handled = true;
-
-
- if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0)
- e.Handled = true;
-
- if (e.KeyChar == 13 || e.KeyChar == (char)Keys.Enter)
- {
- try
- {
- double myDouble = double.parse(TextBox1.Text)
- }
- catch (Exception e)
- {
- MessageBox.Show("Input is incorrect", "Error");
-
-
- TextBox1.Text = "";
- }
- }
-
-
- var regex = new Regex("^[-]?\d+(\.\d+)?$", RegexOptions.Compiled);
- Match m = regex.Match(TextBox1.Text + e.KeyChar);
- if(!m.Success)
- {
- MessageBox.Show("Input is incorrect", "Error");
- }
-
- }