The easiest way to validate data for fields in Windows Forms

I have a project in the form of windows in which I want to force the user to enter values ​​in certain fields before clicking the calculation button below. Fields include three pairs of radio buttons , five text fields and one combo box . Therefore, basically all of these fields should contain a value to perform the calculations. In addition, text fields must contain numbers - any double values. Moreover, I want to set the maximum value for most of these text fields , which the user cannot exceed. Please let me know what is the easiest way to achieve this. I do not see the fields checking the controls for winform projects, similar to those available in ASP.Net. Please note that I am working on .net 3.5. Currently, I use message boxes to inform the user about this, that is, whenever the user clicks β€œcalculate”, I show message boxes with the name of the required fields that are currently empty.

+6
source share
7 answers

I faced the same situation as you, and found a simple solution, or you can say that a simple solution is available for WinForms. WinForms contains an ErrorProvider control that helps us show the error in the required field.

A practical guide. Displaying error icons for form validation is a brief introduction.

ErrorProvider can be used the way you want, for example. for a text field, you can use it in the TextChanged event TextChanged or inside any other event, say, say a button, for example:

 if (!int.TryParse(strNumber, out result)) { errorProvider.SetError(tbNumber, "Only Integers are allowed"); } else { errorProvider.Clear(); } 
+11
source

I think the easiest way to implement all of your custom validation is to have a set of if conditions inside the button click event.

 private void Calculate_button_Click(object sender, EventArgs e) { if(textBox1.Text == string.Empty) { MessageBox.Show("Please enter a value to textBox1!"); return; } else if(!radioButton1.Checked && !radioButton2.Checked) { MessageBox.Show("Please check one radio button!"); return; } else if(comboBox1.SelectedIndex == -1) { MessageBox.Show("Please select a value from comboBox!"); return; } else { // Program logic... } } 

In the same way, you can also check ranges.

+4
source

You can try the following:

 private void Calculate_button_Click(object sender, EventArgs e) { RadioButton[] newRadioButtons = { radiobutton1, radiobutton2, radiobutton3 }; for (int inti = 0; inti < newRadioButtons.Length; inti++) { if (newRadioButton[inti].Checked == false) { MessageBox.Show("Please check the radio button"); newRadioButtons[inti].Focus(); return; } } TextBox[] newTextBox = { txtbox1, txtbox2, txtbox3, txtbox4, txtbox5 }; for (int inti = 0; inti < newRadioButtons.Length; inti++) { if (newTextBox[inti].text == string.Empty) { MessageBox.Show("Please fill the text box"); newTextBox[inti].Focus(); return; } } } 

You can scroll through the controls and find them regardless of whether they are full or not. if they are not filled, they will be displayed in the message box, and special attention will be focused.

+4
source

Try using a control test event and program any necessary test there.

Here is an example that uses some ValidateEmail function to check if the text is an email address, sets the background to some (red?) Color if it does not match, and does not allow the user to leave control as long as it matches.

 Private Sub VoucherEmail_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Textbox.Validating If Not ValidateEmail(sender.Text) Then sender.backcolour = Validation.ErrorBackgrounColor e.Cancel = True End If End Sub 
0
source

I know this is an old topic.

But I think it's worth the answer.

In the button calculation function, click the add button

 if(!this.ValidateChildren()) { return; } 

and add test functions to your

edit

Sorry, this works on .NET 4.0 and above.

0
source
 'You can try this code----- 'Function for null validate for particular type of controls in your form Function NullValidate() As Boolean NullValidate = True For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then If ctrl.Text = "" Then MessageBox.Show("Invalid input for " & ctrl.Name) NullValidate = False Exit Function Else NullValidate = True End If End If Next End Function 'Function for numeric validate for particular type of controls in your form Function NumericValidate() As Boolean NumericValidate = True For Each ctrl As Control In Me.Controls If TypeOf ctrl Is TextBox Then If NumericValidate = IsNumeric(ctrl.text) Then MessageBox.Show("Invalid input for " & ctrl.Name) NumericValidate = False Exit Function Else NumericValidate = True End If End If Next End Function Private Sub cmdCalculate_Click(sender As Object, e As EventArgs) Handles cmdSave.Click If NullValidate() = True Then If NumericValidate() = True Then 'your statement is here End If End If End Sub 
-2
source

Here are the steps:

Step 1. Create a Windows form application.

enter image description here

Step 2: Select the toolbar of the "ErrorProvider" form.

enter image description here

enter image description here

Step 3: Select the text box and go to its properties.

enter image description here

In the properties, select "Events" and in the focus, double-click "Check". Now we have a method for checking a text field.

 private void textBoxName_Validating(object sender, CancelEventArgs e) { if (string.IsNullOrWhiteSpace(textBoxName.Text)) { e.Cancel = true; textBoxName.Focus(); errorProviderApp.SetError(textBoxName, "Name should not be left blank!"); } else { e.Cancel = false; errorProviderApp.SetError(textBoxName, ""); } } 

Step 4: Now the check should be started when you press Enter. Add the following code to the Enter key method.

 private void buttonEnter_Click(object sender, EventArgs e) { if (ValidateChildren(ValidationConstraints.Enabled)) { MessageBox.Show(textBoxName.Text, "Demo App - Message!"); } } 

Also make sure that the CauseValidation Enter button property is set to true.

Now our window form is ready for inspection.

https://www.c-sharpcorner.com/blogs/how-to-use-validation-in-windows-form-application

-2
source

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


All Articles