Dialog Boxes


Using a form as a dialog box is easy. Create the form and give it whatever controls it needs to do its job. Add one or more buttons to let the user dismiss the dialog. Many dialog boxes use OK and Cancel buttons, but you can also use Yes, No, Retry, and others. You may also want to set the form’s FormBorderStyle property to FixedDialog, although that’s not mandatory

Set the form’s AcceptButton property to the button you want to invoke if the user presses the Enter key. Set its CancelButton property to the button you want to invoke when the user presses the Esc key.

The form’s DialogResult property indicates the dialog box’s return value. If the main program displays the dialog box by using its ShowDialog method, ShowDialog returns the DialogResult value. The following code shows how the main program can display a dialog box and react to its result. It creates a new instance of the dlgEmployee form and displays it by calling its ShowDialog method. If the user clicks OK, then ShowDialog returns DialogResult.OK and the program displays the employee’s name entered on the dialog. If the user clicks the Cancel button, ShowDialog returns DialogResult.Cancel and the program displays the message “Canceled.”

  Private Sub btnShowDialog_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnShowDialog.Click     Dim dlg As New dlgEmployee     If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then         MessageBox.Show( _             dlg.txtFirstName.Text & " " & _             dlg.txtLastName.Text)     Else         MessageBox.Show("Canceled")     End If End Sub 

If the user clicks the Cancel button or closes the form by using the system menu, the form automatically sets its DialogResult property to Cancel and closes the form.

If the user clicks some other button, your event handler should set DialogResult to an appropriate value. Setting this value automatically closes the form.

Tip 

You can also set a button’s DialogResult property to indicate the value that the dialog box should return when the user presses that button. When the user presses the button, Visual Basic sets the form’s DialogResult property automatically.

The following code shows how the employee form reacts when the user clicks the OK button. It sees if the first and last name TextBox controls contain nonblank values. If either value is blank, the event handler displays an error message and returns without setting the form’s DialogResult property. If both values are nonblank, the code sets DialogResult to OK, and setting DialogResult closes the form. Note that the dialog box doesn’t need an event handler for the Cancel button. If the user clicks Cancel, Visual Basic automatically sets the form’s DialogResult to Cancel and closes the form.

  Private Sub btnOk_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnOk.Click     ' Verify that the first name is present.     If txtFirstName.Text.Length = 0 Then         MessageBox.Show( _             "Please enter a First Name", _             "First Name Required", _             MessageBoxButtons.OK, _             MessageBoxIcon.Exclamation)         txtFirstName.Select()         Exit Sub     End If     ' Verify that the last name is present.     If txtLastName.Text.Length = 0 Then         MessageBox.Show( _             "Please enter a Last Name", _             "Last Name Required", _             MessageBoxButtons.OK, _             MessageBoxIcon.Exclamation)         txtLastName.Select()         Exit Sub     End If     ' Accept the dialog.     Me.DialogResult = Windows.Forms.DialogResult.OK End Sub  

Many dialog boxes provide OK and Cancel buttons, so they usually set DialogResult to OK or Cancel. However, you can also set DialogResult to Abort, Ignore, No, None, Retry, and Yes if that makes sense for your program. The main program can use an If Then or Select Case statement to see which value was set.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

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