Monday, November 26, 2012

Integer validation using ErrorProvider in C# Windows Application

C# Windows Application provides an ErrorProvider control, which is similar to the ValidationSummary control in Asp.Net, this control consolidates all the error messages in the form and alerts the user on the Form errors.

In this post we shall see on how to use the ErrorProvider control to validate Integers (Whole Numbers) in a C# Windows Form.

1. Create a new / open an existing C# Windows application
2. Open and existing / Add a new Windows Form to the Project
3. Add the required controls to the Form, let us add a TextBox which accepts only Numbers.
4. Open the ToolBar, Drag an ErrorProvider control into the form.
5. The ErrorProvider control can be found in the Components section.
5. The control gets added to the control tray at the bottom of the Form in the Design View.


6. Provide a name to the ErrorProvider, let us give is as errSummary
7. Now in the Validating event of the TextBox, we shall add code to validate the TextBox as follows.



        boolFormValid = true;
        //
        private void txtAge_Validating(object sender, CancelEventArgs e)
        {
            ValidateAge();
        }       
        //
        private void ValidateAge(object sender, CancelEventArgs e)
        {
            // Check if the Field is Not Empty.
            if (txtAge.Text == "")
            {
                errSummary.SetError(txtAge, "Please enter Age.");
                boolFormValid = false;
                return;
            }
            //
            // Check if the user has entered a valid Number.
            try
            {
                int temp = int.Parse(txtAge.Text);
            }
            catch
            {
                errSummary.SetError(txtAge, "Please enter a Valid Number.");
                boolFormValid = false;
                return;
            }
            errSummary.SetError(txtAge, "");
        }

8. Once we set the control and Error Message to the ErrorProvider control, an Alert icon is displayed to the user and the error message is displayed as a ToolTip as follows.

9. The
boolFormValid is a Form level bool variable, which is set to false, when any of the validations fail, while submitting the form, make sure that this is true, else alert the user to correct the validation errors.


        private void cmdSubmit_Click(object sender, EventArgs e)
        {
            boolFormValid = true;
            ValidateAge();
            //
            if (!boolFormValid)
            {
                MessageBox.Show("Please correct the Validation Error.");
                return;
            }
       }

10. The _Validating event will get fired only when the focus is moved out of the TextBox, if you want the error message to be displayed instantly, then we can use the _TextChanged event handler, this will get fired when any text is entered/changed in the textbox.

        private void txtAge_TextChanged(object sender, CancelEventArgs e)
        {
            ValidateAge ();
        }        

Search Flipkart Products:
Flipkart.com

No comments: