Allow only numbers and negative values

I have a textbox that needs to accept only numbers (there can be decimal values) and negative values.

I currently have something similar in KeyPress event

  if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') { e.Handled = true; } 

What else should I do to resolve negative values?

thanks

+4
source share
6 answers
 if (!char.IsControl(e.KeyChar) && (!char.IsDigit(e.KeyChar)) && (e.KeyChar != '.') && (e.KeyChar != '-')) e.Handled = true; // only allow one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1) e.Handled = true; // only allow minus sign at the beginning if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0) e.Handled = true; 

As correctly said in the comments on LB, this will not allow the use of some advanced notation like 3E-2 , but for primes it will do the trick.

+7
source

I thought the text field has a property in which you can set the input to what is being inserted. Although I currently cannot verify this.

otherwise, as an alternative, you might try to parse your input into double when sending the value. Sort of:

 double myDouble; try { myDouble = double.parse(textbox.Text) } catch (Exception e) { MessageBox.Show("Input is incorrect", "Error") } 

this is probably not the best workaround, but it may just do the trick.

+1
source

Connect a test event, for example:

 private void myTextBox_Validating(object sender, CancelEventArgs event) { decimal d; if(!decimal.TryParse(myTextBox.Text, out d) { event.Cancel = true; //this.errorProvider1.SetError(myTextBox, "My Text Box must be a negative number."); //optional return; } if(d >= 0) { event.Cancel = true; //this.errorProvider1.SetError(myTextBox, "My Text Box must be a negative number."); //optional return; } } 
+1
source

Sort of:

 var regex = new Regex("^[-]?\d+(\.\d+)?$", RegexOptions.Compiled); Match m = regex.Match(textbox.Text + e.KeyChar); e.Handled = m.Success; 

Edit : now it allows any real number

0
source
 // Boolean flag used to determine when a character other than a number is entered. private bool nonNumberEntered = false; // Handle the KeyDown event to determine the type of character entered into the control. private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) { // Initialize the flag to false. nonNumberEntered = false; // Determine whether the keystroke is a number from the top of the keyboard. if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9) { // Determine whether the keystroke is a number from the keypad. if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9) { // Determine whether the keystroke is a backspace. if(e.KeyCode != Keys.Back) { // A non-numerical keystroke was pressed. // Set the flag to true and evaluate in KeyPress event. nonNumberEntered = true; } } } //If shift key was pressed, it not a number. if (Control.ModifierKeys == Keys.Shift) { nonNumberEntered = true; } } // This event occurs after the KeyDown event and can be used to prevent // characters from entering the control. private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { // Check for the flag being set in the KeyDown event. if (nonNumberEntered == true) { // Stop the character from being entered into the control since it is non-numerical. e.Handled = true; } } 
0
source

Try this regex

 "/^(?!0*[.,]0*$|[.,]0*$|0*$)\d+[,.]?\d{0,2}$/" 

and use the System.Text.RegularExpressions Namespace

See an example here: http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

0
source

Source: https://habr.com/ru/post/1443223/


All Articles