The easiest way to add a dialog box to your application is to use a predefined dialog, because you don't have to worry about designing, loading, or showing the dialog box. However, your control over its appearance is limited. Predefined dialog boxes are always modal.
The following table lists the functions you can use to add predefined dialog boxes to your Visual Basic application.
Use this function | To do this |
InputBox function | Display a command prompt in a dialog box, and return whatever is entered by the user. |
MsgBox function | Display a message in a dialog box, and return a value indicating the command button was clicked by the user. |
Use the InputBox function to solicit data from the user. This function displays a modal dialog box that asks the user to enter some data. The text input box shown in Figure 6.17 prompts the user for the name of the file to open.
Figure 6.17 A dialog box using the InputBox function
The following code displays the input box shown in Figure 6.17:
FileName = InputBox("Enter file to open:", "File Open")
Note Remember that when you use the InputBox function, you have little control over the components of the dialog box. You can change only the text in the title bar, the command prompt displayed to the user, the position of the dialog box on the screen, and whether or not it displays a Help button.
For More Information See "InputBox Function" in the Language Reference.
Use the MsgBox function to get yes or no responses from users, and to display brief messages, such as errors, warnings, or alerts in a dialog box. After reading the message, the user chooses a button to close the dialog box.
An application named Text Editor might display the message dialog box shown in Figure 6.18 if a file cannot be opened.
Figure 6.18 An error message dialog box created using the MsgBox function
The following code displays the message box shown in Figure 6.18:
MsgBox "Error encountered while trying to open file, _
please retry.", vbExclamation, "Text Editor"
Note Modality can either be limited to the application or the system. If a message box's modality is limited to the application (default), then users cannot switch to another part of the application until the dialog box is dismissed, but they can switch to another application. A system modal message box does not allow the user to switch to another application until the message box is dismissed.
For More Information See "MsgBox Function" in the Language Reference.