C # - correct integer checking

I am currently creating my project using window forms and came across a small “problem”.

I have a user who enters an hour that is stored as an int. I want to provide detailed feedback to the user so that they know exactly what they did wrong if they cause an error.

If no value is specified, a format exception is thrown. If only an integer is specified, a format exception is thrown.

This means that I cannot directly tell the user that the new item cannot be added due to EITHER 1) there is no value or 2) not an integer, since both of them use the same exception.

How can I solve this and what will be the best solution?

Many thanks.

+4
source share
5 answers

example code related to your question; note ValidateData in particular:

// called from ok button click or similar event private void Accept() { if (!ValidateData()) return; SaveData(); DialogResult = DialogResult.Ok; Dispose(); } private bool ValidateData() { int val; if (string.IsNullOrEmpty(mTextBox.Text)) return FailValidation("Value can not be empty.", mTextBox); if (!int.TryParse(mTextBox.Text, out val)) return FailValidation("Value was not an integer.", mTextBox); return true; } // do something with the value if you need private void SaveData() { } // post a message to the user, and highlight the problematic control // always evaluates to false private bool FailValidation(string pMessage, Control pControl) { if (pControl != null) { pControl.Focus(); TextBox textBox = pControl as TextBox; if (textBox != null) textBox.SelectAll(); } AlertBox(pMessage); return false; } // quick alert message method private void AlertBox(string pMessage) { return MessageBox.Show ( pMessage, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1 ); } 
+1
source

Use the Int32.TryParse Method and check the return value. You can simply check the value not entered before calling TryParse.

Here is a usage example from MSDN:

  int number; bool result = Int32.TryParse(value, out number); if (result) { Console.WriteLine("Converted '{0}' to {1}.", value, number); } else { if (value == null) value = ""; Console.WriteLine("Attempted conversion of '{0}' failed.", value); } 
+2
source

Use int.TryParse to check the format and check if the integer is in the valid range. Use String.IsNulOrEmpty to check for an empty string.

+1
source

If I can offer a possible alternative solution ... The best test is to prevent bad input in the first place. Can you limit the values ​​that the user can select using a control, such as a time collector or drop-down list? The drop-down list will still be keyboard friendly for powerusers, and it's a bit easier for those who prefer a mouse. Winning for everyone.

+1
source

This is well supported in Winforms. Use the Validating event to check the record, the ErrorProvider component to report an error. Example event handler:

  private void textBox1_Validating(object sender, CancelEventArgs e) { int hour; e.Cancel = true; if (textBox1.Text.Length == 0) errorProvider1.SetError(textBox1, "Can't be empty"); else if (!int.TryParse(textBox1.Text, out hour)) errorProvider1.SetError(textBox1, "Not a number"); else if (hour < 1) errorProvider1.SetError(textBox1, "Hour too small"); else if (hour > 24) errorProvider1.SetError(textBox1, "Hour too large"); else { e.Cancel = false; errorProvider1.SetError(textBox1, ""); } } 

Then you just need to check if all entries were satisfactory. Use the ValidateChildren () method in the OK Button Event Handler dialog box:

  private void OKButton_Click(object sender, EventArgs e) { if (ValidateChildren()) this.DialogResult = DialogResult.OK; } 
+1
source

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


All Articles