Dialogs


You've already seen how to create and show forms, but there is a special usage of forms that show as dialogs. Although it's not always the case, dialogs are typically modal and exist to take information from a user before a task can be completed ”in other words, a dialog is a form that has a "dialog" with the user . For example, the Options dialog in Figure 1.15 was created by right-clicking on a project in Solutions Explorer and choosing Add Windows Form. Implementing the form was a matter of exposing the favorite color setting as a property, dropping the controls onto the form's design surface, and setting the ControlBox property to false so that it looks like a dialog.

Figure 1.15. A Dialog Box

You can use this form as a modal dialog by calling the ShowDialog method:

 void viewOptionsMenuItem_Click(object sender, EventArgs e) {   MyOptionsDialog dlg = new MyOptionsDialog();   dlg.FavoriteColor = this.color;   if(  dlg.ShowDialog() == DialogResult.OK  ) {     this.color = dlg.FavoriteColor;   } } 

Notice that an instance of the custom class, MyOptionsDialog, is created, but before it's shown, the initial values are passed in via a property. When the modal ShowDialog method returns, it provides a member of the DialogResult enumeration, either OK or Cancel in this case. Although it's possible to implement the OK and Cancel buttons' Click events inside the MyOptionsDialog class, there's a much easier way to make OK and Cancel act as they should: You set each button's DialogResult property appropriately, and set the MyOptionsDialog form properties AcceptButton and CancelButton to refer to the appropriate buttons . In addition to closing the dialog and returning the result to the caller of ShowDialog, setting these properties enables the Enter and ESC keys and highlights the OK button as the default button on the form.

You may still feel the need to handle the OK click event to validate the data typed into the dialog. Although you can do that, WinForms provides built-in support for validation. By using an ErrorProvider component, along with the Validating event, you can validate the contents of each control when the user moves focus from that control. For example, if we want the user to specify a color with some green in it, we can drop an ErrorProvider component onto the MyOptionsDialog form and handle the Validating event for the Change button whenever it loses focus:

 void changeColorButton_Validating(object sender, CancelEventArgs e) {   byte greenness = changeColorButton.BackColor.G;   string err =  "";  if( greenness < Color.LightGreen.G ) {     err = "I'm sorry, we were going for leafy, leafy...";  e.Cancel = true;  }  errorProvider1.SetError(changeColorButton, err);  } 

In the Validating handler, notice that we set the CancelEventArgs Cancel property to true. This cancels the loss of focus from the control that caused the validating event and stops the dialog from closing. Also notice the call to ErrorProvider.SetError. When this string is empty, the error provider's error indicator for that control is hidden. When this string contains something, the error provider shows an icon to the right of the control and provides a tooltip with the error string, as shown in Figure 1.16.

Figure 1.16. ErrorProvider Providing an Error

The Validating event handler is called whenever focus is moved from a control whose CausesValidation property is set to true (the default) to another control whose CausesValidation property is set to true. To let the user cancel your dialog without entering valid data, make sure to set the Cancel button's CausesValidation property to false, or else you'll have pretty frustrated users.

ErrorProvider and the Validating event provide most of what's needed for basic validation, but more complicated validation scenarios require some custom coding. Similarly, because not all dialogs are modal, you'll need other ways of communicating user settings between your dialog and the rest of your application. For a discussion of these issues, as well as a list of the standard dialogs and how to use them, you'll want to read Chapter 3: Dialogs.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net