Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

VALIDATIONS AND EXCEPTION HANDLING

Chapter 9

 

183

 

 

 

 

 

The preceding code displays an error message if the user specifies incorrect values for the editCarNo and the editWorkerId fields. You will notice that I have not validated the dates and other fields. It is easier to validate these fields by using exception handlers, which will be discussed in the “Handling Exceptions” section of this chapter. Before doing that, the next section will examine the ErrorProvider control of Visual Studio .NET.

Using the ErrorProvider Control

Instead of displaying message boxes each time the user submits an incorrect or incomplete form, you can use the ErrorProvider control to show an icon next to the control (which will be referred to as the error icon in future references) that has an error. When a user moves the mouse pointer over the error icon, the error message associated with the icon is displayed as a ToolTip.

The ErrorProvider control enhances user experience by eliminating the use of message boxes for notifying errors. You have already added the validation code for the JobDetails form.Therefore, in this section, you will validate a different form, CustomerForm, by using the ErrorProvider control. The steps to add the ErrorProvider control to the CustomerForm form are as follows:

1.Open the CustomerForm form in the Design view.

2.Drag the ErrorProvider control from the Toolbox to the form. The ErrorProvider control will be added to the component tray.

3.Change the Name property of ErrorProvider to errCustForm.

4.Click on the textBox1 control that represents the Car No. field.

5.In the Properties window, specify a description of the error message in the Error on errCustForm property, as shown in Figure 9-6.

6.Repeat Steps 4 and 5 to add error descriptions for all text boxes to the

CustomerForm form.

184 Project 1 CREATING A CUSTOMER MAINTENANCE PROJECT

FIGURE 9-6 Adding an ErrorProvider control

TIP

As you add error descriptions to each control in the form, an exclamation point icon appears next to each control.

When you specify an error message with each control during design time, the error icon appears as soon as a user loads the form. To avoid showing an error message even before the user has entered values in the form, you should clear the error message. To clear the error messages associated with controls, use the SetError method of the errCustForm control.The SetError method sets the error message associated with a control. If a blank string is passed to this method, the error message associated with the control is cleared. To clear error messages, add the following code for the Load event

of the CustomerForm form:

private void CustomerForm_Load(object sender, System.EventArgs e)

{

errCustForm.SetError(textBox1,””); errCustForm.SetError(textBox2,””); errCustForm.SetError(textBox3,””); errCustForm.SetError(textBox4,””);

}

VALIDATIONS AND EXCEPTION HANDLING

Chapter 9

185

 

 

 

Next, you need to check for the availability of data in each text box when the user clicks on Save.The code for the Click event of the Save button is given as follows:

private void btnSave_Click(object sender, System.EventArgs e)

{

bool flag; flag=true;

if (textBox1.Text==””)

{

errCustForm.SetError(textBox1,”Please specify a valid car number.”); flag=false;

}

else errCustForm.SetError(textBox1,””);

if (textBox2.Text==””)

{

errCustForm.SetError(textBox2,”Please specify a valid name.”); flag=false;

}

else errCustForm.SetError(textBox2,””);

if (textBox3.Text==””)

{

errCustForm.SetError(textBox3,”Please specify a valid address.”); flag=false;

}

else errCustForm.SetError(textBox3,””);

if (textBox4.Text==””)

{

errCustForm.SetError(textBox4,”Please specify a valid make.”); flag=false;

}

else errCustForm.SetError(textBox4,””);

if (flag==false) return;