Friday, November 23, 2012

Number 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 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.


6. The control gets added to the control tray at the bottom of the Form in the Design View.
7. Provide a name to the ErrorProvider, let us give is as
errSummary8. Now in the Validating event of the TextBox, we shall add code to validate the TextBox as follows.




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

9. 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.

10. 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;
            ValidateWeight();
            ValidateHeight();
            //
            if (!boolFormValid)
            {
                MessageBox.Show("Please correct the Validation Error.");
                return;
            }
        }



11. 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 txtWeightKg_TextChanged(object sender, CancelEventArgs e)
        {
            ValidateWeight();
        }        

Search Flipkart Products:
Flipkart.com

No comments: