Sometimes, nothing will do but to create your own dialog boxes. Visual Basic supports message boxes and input boxes, but they're very basic—in real applications, you'll need to create custom dialog boxes.
To see how this works, create a new Windows application now; I'll call this example Dialog on the CD-ROM. In this example, I'll let the user enter some text in a dialog box, and read the entered text when the dialog box is closed.
To create the dialog box, add a new Windows form, Form2, to the project now, and add two buttons with the captions OK and Cancel to this form, as well as a text box, as you see in Figure 4.18. I've set the Text property of this form to "Enter your text" to set the text in the title bar, and added a label control above the text box with the prompt "Enter your text:". Label controls just display text like this prompt. (We'll see them in Chapter 5.) To create this label, just drag a Label control from the toolbox to Form2 and set its Text property.
In addition, set the FormBorderStyle property of Form2 to FixedDialog, giving it a dialog box border, and set the ControlBox property to False to remove the control box (the minimize, maximize, and close buttons at upper right). Also, set the ShowInTaskbar property of Form2 to False—this means that when this dialog box appears, it will not display an icon in the Windows task bar, which dialog boxes shouldn't.
Finally, set the DialogResult property of the OK button to OK, and the same property of the Cancel button to Cancel. This property returns a value of the DialogResult enumeration when the dialog box is closed, so you can determine which button the user has clicked. Here are the possible settings for this property:
We'll need some way of displaying our new dialog box from Form1, the form that appears when the application starts, so add a button, Button1, to Form1 now, giving it the text "Enter your text". Also, we'll need a way of displaying the text we read from the dialog, so add a text box, TextBox1, to Form1 now.
To display the dialog box when the user clicks the "Enter your text" button, I'll create a new object of the Form2 dialog box class, DialogBox. To display this dialog box, I'll use the ShowDialog method, not the Show method, because ShowDialog will return a DialogResult value indicating what button the user clicked. If the user clicked the OK button, I'll display the text from the text box in the dialog box in the main form:
Public Class Form1 Inherits System.Windows.Forms.Form Dim DialogBox As New Form2() 'Windows Form Designer generated code Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click If DialogBox.ShowDialog = DialogResult.OK Then TextBox1.Text = DialogBox.TextBox1.Text End If End Sub End Class
The last step is to add some code to the dialog box, Form2, to close the dialog box when the user clicks a button. Also, I'll set the dialog box's AcceptButton and CancelButton properties to indicate which button is the accept button and which the Cancel button; this allows the user to press Enter to select the accept (OK) button, and Esc to select the Cancel button:
Public Class Form2 Inherits System.Windows.Forms.Form 'Windows Form Designer generated code Private Sub Form2_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.AcceptButton = Button1 Me.CancelButton = Button2 End Sub Private Sub Button1_Click(ByVal sender As System.Object, _\ ByVal e As System.EventArgs) Handles Button1.Click Me.Close() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub End Class
And that completes the code—now run the application, as shown in Figure 4.19. The dialog box appears when you click the button in Form1, and you can enter text in it. When you click the OK button, the dialog box disappears and the text you entered appears in the text box in Form1, as you see in Figure 4.20. Everything works as we planned it.
One good rule for constructing dialog boxes—always add a Cancel button so that if the user has opened the dialog box by mistake, they can close it without consequences.