Error Provider in C#


Basically we use an Error Provider control to drive attention to a specific control if a user forgets to put any input information that is required.  We can substitute it by a message box. For example, if the name text box is empty on a User Information window and when user tries to save the information, a message pops up saying "Please Enter a valid name" and Name text box gets the focus.

Error Provider does same work but the visualization is much better. If there is any empty field and it requires data then after clicking it blinks and user gets better attention. 

Here are a few screen shots.

1. Add Error Provider in Design window

ErrorProviderImg1.jpg  

 2. If all the fields are empty then it shows like this:
ErrorProviderImg2.jpg

3. If we give some data and others fields are still empty then it looks like:

ErrorProviderImg3.jpg

4. When we enter all the recommended field then it's ok & do button events like:

ErrorProviderImg4.jpg

The codes are as follows

        private void button1_Click(object sender, EventArgs e)

        {

            doWork();

  }

        public void doWork()

        {

            bool error = false;

 

            if (textBox1.Text == "")

            {

                errorProvider1.SetError(textBox1, "Please enter your First Name!");

                error = true;

            }

            else

            {

                errorProvider1.SetError(textBox1, "");

                error = false;

            }

 

            if (textBox2.Text == "")

            {

                errorProvider1.SetError(textBox2, "Please enter your Last Name!");

                error = true;

            }

            else

            {

                errorProvider1.SetError(textBox2, "");

                if (error != true)// has no any previous error then set error false because if it has any single one error then it should not execute

                    error = false;

            }

 

            if (textBox3.Text == "")

            {

                errorProvider1.SetError(textBox3, "Please enter your Occupation!");

                error = true;

            }

            else

            {

                errorProvider1.SetError(textBox3, "");

                if (error != true)// has no any previous error then set error false because if it has any single one error then it should not execute

                    error = false;

            }

 

            if (textBox4.Text == "")

            {

                errorProvider1.SetError(textBox4, "Please enter your Country Name!");

                error = true;

            }

            else

            {

                errorProvider1.SetError(textBox4, "");

                if (error != true)// has no any previous error then set error false because if it has any single one error then it should not execute

                    error = false;

            }

 

            // finally what you want to do after getting all the information

            if (error == false)

            {

                MessageBox.Show("All Information are collected!","Complete",MessageBoxButtons.OK,MessageBoxIcon.Information);

            }

        }

 

That's it.

Thank you!   

Up Next
    Ebook Download
    View all
    Learn
    View all