« Home « Kết quả tìm kiếm

Doing Lesson with Customer Maintenance


Tóm tắt Xem thử

- You decide to create a form like the one shown in the following graphic..
- You need to ensure that the user's input is consistent.
- the title (Mr, Mrs, Miss, or Ms) must match the selected gender (Male or Female), and vice versa..
- In the following exercises, you will examine the Customer Maintenance application and run it to see how easily you can get validation wrong..
- Open the CustomerDetails project, located in the \Microsoft Press\Visual CSharp Step By Step\ Chapter 22\CustomerDetails folder in your My Documents folder..
- In the Solution Explorer, double-click CustomerForm.cs to display the Customer Details form in Design View..
- Click the Title combo box on the form, and then click the Items property in the Properties window.
- Click the Ellipses button to display the strings in the collection..
- You can see from this collection that the list box contains four titles: Mr, Mrs, Miss, and Ms.
- Examine the Gender group box and radio buttons.
- The application enforces the business rule that the gender and the title must match..
- If the title is Mr, the gender must be male, and if the title is Mrs, Miss, or Ms, the gender must be female..
- Cross check the gender and the title to make sure they correspond 7.
- MessageBox.Show("If the title is Mr the gender must be male Error", 15.
- Check that the gender is Female 22.
- MessageBox.Show("If the title is Mrs, Miss, or Ms 25.
- the gender must be female Error",.
- This method performs a simple cross-check between the contents of the Title combo box and the radio buttons in the Gender group box.
- If the title is “Mr,” the business rule states that the gender must be male, and the method checks to ensure that the Male radio button is selected.
- Likewise, if the title is one of the other values (Mrs, Miss, or Ms), the business rule states that the gender must be female, and the method looks at the Female radio button to ensure it is selected.
- Again, if this is not the case, a different error message is displayed and the method returns false.
- If the title and the gender match, the method returns true..
- These are implementations of the Validating event handler for the Title combo box and the Gender group box.
- They both call the checkTitleAndGender method, and then set the Cancel property of the CancelEventArgs parameter to true if the checkTitleAndGender method returns false:.
- The saveClick method displays the message “Customer Saved” (this is just a prototype form—the production version would actually save the information somewhere)..
- Notice that the default gender is Male..
- Select Mrs from the Title combo box and tab to or click the first Name text box..
- Notice that the checkTitleAndGender method generates and displays an error message because the title and the gender don't agree..
- Click OK in the Error dialog box to close it, and then try to click the Female radio button in the group box..
- You will fail, as the CausesValidation property of the group box makes the Validating event run again, causing the error message to be displayed once more..
- Click OK in the Error dialog box.
- Set the title to Mr and click the Female radio button..
- Remember that the Validating event fires just before the focus is passed to the Female radio button.
- When the checkTitleAndGender method is called, the title (Mr) and the gender (male) will agree.
- You are successfully able to set the gender to female..
- Now that you have now set the gender to female, try to save the customer's details by clicking Save in the File menu.
- This action works without error and the.
- Correct the (non-reported) error by trying to set the title to Mrs..
- The Validating event (this time for the Gender group box) runs, spots that the gender and title don't match, and then displays an error.
- You will not be able to escape until you set the Gender to Male again (but then you are back to square one)..
- The issue with the Customer Maintenance application is that the validation is performed at the wrong time, is inconsistently applied, and interferes too much.
- This way you can ensure that the user has finished entering all the data and that it is consistent..
- In the following exercise, you will change the Customer Maintenance application to postpone validation until the customer information is saved..
- Click the Title combo box..
- In the Properties window, click the Events button.
- This unsubscribes the Title combo box from the Validating event..
- On the CustomerForm, select the Gender group box..
- Unsubscribing from an event in this way detaches the event method from the event itself but does not delete the event method—if you no longer need the method, you can remove it manually in the Code And Text Editor window..
- Display the file CustomerForm.cs in the Code And Text Editor window.
- Delete the title-Validating and genderValidating methods.
- If the checkTitleAndGender method returns false, an error message box is displayed and the customer's details are not saved..
- When the Customer Details form appears, set the Title combo box to Mrs, and then click in the first Name text box.
- This should work without error because the Validating event is no longer trapped by the Title combo box..
- Verify that the Male radio button is selected, and then click the Save item in the File menu.
- Select the Female radio button, and then click the Save Customer button on the toolbar..
- A much better technique is to use an ErrorProvider control as shown in the following exercise..
- Return to CustomerForm in the Design View window..
- In the Toolbox, expand the Components category.
- Click the ErrorProvider control and drop it anywhere on the form..
- Click the errorProvider1 control, and select the Properties window.
- Change the (Name) property to errorProvider and verify that the BlinkStyle property is set to BlinkIfDifferentError..
- You can change the BlinkRate property if you want to make it blink faster or slower—the default rate is 250 milliseconds (four times a second).
- On the CustomerForm, select the Title combo box.
- an error icon appears next to the Title combo box.
- message) only in the event of an error, so delete the text “Testing” from the Error on errorProvider property.
- MessageBox.Show with invocations of the errorProvider.SetError method, as shown here:.
- Cross check the gender and the title to make sure they correspond 6.
- errorProvider.SetError(gender, "If the title is Mr ".
- "the gender must be male");.
- errorProvider.SetError(title, "If the gender is ".
- "female the title must be Mrs, Miss, or Ms");.
- Check that the gender is Female 23.
- errorProvider.SetError(gender, "If the title is Mrs, ".
- "Miss, or Ms the gender must be female");.
- errorProvider.SetError(title, "If the gender is male ".
- "the title must be Mr");.
- Select Mrs in the Title combo box, and then verify that the Male radio button is selected..
- On the File menu, click Save.
- A message box appears indicating that the save failed, and error icons are displayed next to the controls that are in error.
- If you hover the mouse pointer over each of the error icons, you see the error message, as shown in the graphic on the next page..
- Select the Female gender radio button and click the Save item on the File menu..
- As the data is now consistent, you will see the Customer Saved message box and the error icons will disappear..
- Adding a Status Bar.
- This is feedback that the user needs to be able to see, but it is annoying to the user to have to click the OK button to acknowledge the messages.
- In the final exercise in this chapter, you will implement a status bar in the CustomerForm form by using a StatusStrip control..
- In the Toolbar, expand the Menus &.
- The control appears underneath the form, and a status bar is added to the base of the form..
- Click the statusStrip1 control, and select the Properties window.
- In the status bar on the form, click the drop-down arrow as shown in the following graphic, and click StatusLabel..
- Click the toolStripStatusLabel1 control in the status bar.
- The control shrinks in size, but is still present in the status bar..
- Change the first MessageBox statement (in the if statement) to display a message in the status bar instead, as follows:.
- You will see why you need to set the text color to black in the next step..
- Change the second MessageBox statement (in the else statement) to display the following error message in the status bar:.
- Test the status bar.
- (A finished version is available in the \Microsoft Press\Visual CSharp Step By Step\Chapter 22\CustomerDetails Complete folder in your My Documents folder, if you need it.).
- On the File menu, click Save..
- A red error message appears in the status bar indicating that the save failed.
- You will see the Customer Saved message in the status bar (in black) and the error icons will disappear..
- On the File menu, click Exit

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt