Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to perform data validation for fields on Windows Forms

I have a windows form project in which I want to force the user to enter values in certain fields before he presses the calculate button at the bottom. The fields include three pairs of radio buttons, five text boxes and one combo box. So basically all these fields need to contain a value in order to perform the calculations. Additionally, the text boxes should contain numbers - any double values. Moreover, I want to set a maximum value set for most of these text boxes which the user cannot exceed. Please let me know what is the simplest way to achieve this. I don't see field validating controls for winform projects like those available in ASP.Net. Please note, I am working on .net 3.5. Currently, I am using the message boxes to communicate this to the user i.e. whenever the user does press calculate I display message boxes mentioning the name of the required fields which are presently empty.

like image 296
R.S.K Avatar asked Nov 23 '12 11:11

R.S.K


3 Answers

I came across same situation as you, and I found an easy solution or you can say that easy solution available for WinForms. WinForms contains a control ErrorProvider which will facilitate us to show error on the required field.

The How to: Display Error Icons for Form Validation provides a short introduction.

ErrorProvider can be used the way you want to e.g. for a textbox you can use it in the TextChanged event handler or inside any other let's say button's event, like so:

if (!int.TryParse(strNumber, out result))
{
    errorProvider.SetError(tbNumber, "Only Integers are allowed");
}
else
{
    errorProvider.Clear();
}
like image 152
Zeeshan Ajmal Avatar answered Nov 14 '22 13:11

Zeeshan Ajmal


I guess the easiest way to implement all 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 check the ranges as well.

like image 26
CRoshanLG Avatar answered Nov 14 '22 11:11

CRoshanLG


You can try this :

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 loop through the controls and find them whether they are filled or not if they are not filled it will show in message box and particular control will be focused.

like image 5
gabriel prakash Avatar answered Nov 14 '22 13:11

gabriel prakash