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 box. 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 to make the form non-resizable, although that’s not mandatory.

Set the form’s AcceptButton property to the button that 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 CustomDialog example program, which is available for download on the book’s website, uses the following code to display a dialog box and react to its result.

Private Sub btnShowDialog_Click() 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

This code creates a new instance of the dlgEmployee form and displays it by calling its ShowDialog method. If the user clicks OK, ShowDialog returns DialogResult.OK and the program displays the first and last names entered on the dialog box. If the user clicks the Cancel button, ShowDialog returns DialogResult.Cancel and the program displays the message “Canceled.”

If the user clicks the Cancel button or closes the form by using the system menu (or the little “X” in the upper-right corner), 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.


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

The following code shows how the dlgEmployee form reacts when the user clicks the OK button. It checks whether the first and last name TextBox controls contain non-blank 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 non-blank, the code sets DialogResult to OK, and setting DialogResult closes the form.

Private Sub btnOk_Click() 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

CANCEL WITHOUT EVENTS
Note that the dialog box doesn’t need an event handler for the Cancel button. If you set the form’s CancelButton property to the button and if the user clicks it, Visual Basic automatically sets the form’s DialogResult to Cancel and closes the form.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.4.181