Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Creating Dialog Boxes

It’s time to ask the user for some feedback, and you don’t want to use the Visual Basic input box because that can only accept one line of text. Besides, you don’t like the way it looks (it’s not a great favorite among Visual Basic programmers, perhaps for that reason). Looks like you’ll have to use a dialog box. How do they work in Visual Basic?

To add a dialog box to a project, select the Project[vbar]Add Form item. You can add a simple form and make it into a dialog box, but Visual Basic already has a predefined dialog box form, named Dialog, so select that in the Add Form box and click Open.


TIP:  To learn more about adding predefined forms to a project, see “Using Visual Basic Predefined Forms, Menus, And Projects” in Chapter 2.

This adds a new dialog box to the project, as shown in Figure 4.21.


Figure 4.21  A new dialog box.

This dialog box comes with an OK and Cancel button, and its BorderStyle property is already set to 3, which creates a fixed dialog-style border with only one control button: a close button.

We add a text box, Text1, to the dialog box, as also shown in Figure 4.21. Next, we declare a Public string, Feedback, in the dialog box’s (General) section; this string will hold the text that the user gives us as feedback:

Public Feedback As String

When the dialog box opens, we can initialize Feedback to the empty string:

Private Sub Form_Load()
    Feedback = ""
End Sub

If the user clicks the Cancel button, we want to leave the text in Feedback as the empty string and just hide the dialog box:

Private Sub CancelButton_Click()
    Hide
End Sub

If the user clicks OK, on the other hand, we fill the Feedback string with what the user has typed into the text box, and then hide the dialog box:

Private Sub OKButton_Click()
    Feedback = Text1.Text
    Hide
End Sub

That completes the dialog box. In the program’s main form, we can show that dialog box when required this way—note that we pass a value of 1 to the Show() method, which displays our dialog box as modal. Modal means that the user must dismiss the dialog box before continuing on with the rest of the program (the default value passed to Show() is 0, which displays windows in a non-modal way):

Private Sub Command1_Click()
    Dialog.Show 1
...
End Sub

Next, we can display the feedback that the user has given us, if any, by examining the dialog’s Feedback string this way:

Private Sub Command1_Click()
    Dialog.Show 1
    Text1.Text = Dialog.Feedback
End Sub

And that’s it—now we are supporting dialog boxes, as shown in Figure 4.22.


Figure 4.22  Using a newly created dialog box.


TIP:  One good rule for constructing dialog boxes: always add a Cancel button so that if users open the dialog box by mistake, they can close it without consequences.

All About Message Boxes And Input Boxes

Visual Basic provides two ways of displaying message boxes and input dialog boxes: using MsgBox() and InputBox(). We’ll cover their syntax in the following subsections.

The MsgBox() Function

You use MsgBox() to display a message to the user and get a return value corresponding to one of the buttons in the message box. Here’s the syntax:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The prompt argument holds the string displayed as the message in the dialog box. (The maximum length of prompt is approximately 1,024 characters.)


TIP:  If prompt is made up of more than one line, you can separate the lines using a carriage return character (Chr(13) ), a linefeed character (Chr(10) ), or both (Chr(13) & Chr(10) ) between each line.

The buttons argument specifies what to put into the message box, as specified in Table 4.2. The default value for buttons is 0.

Table 4.2 MsgBox() constants.
Constant Value Description
vbOKOnly 0 Display OK button only
vbOKCancel 1 Display OK and Cancel buttons
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons
vbYesNoCancel 3 Display Yes, No, and Cancel buttons
vbYesNo 4 Display Yes and No buttons
vbRetryCancel 5 Display Retry and Cancel buttons
vbCritical 16 Display Critical Message icon
vbQuestion 32 Display Warning Query icon
vbExclamation 48 Display Warning Message icon
vbInformation 64 Display Information Message icon
vbDefaultButton1 0 First button is default
vbDefaultButton2 256 Second button is default
vbDefaultButton3 512 Third button is default
vbDefaultButton4 768 Fourth button is default
vbApplicationModal 0 Application modal; the user must respond to the message box before continuing work in the current application.
vbSystemModal 4096 System modal; all applications are suspended until the user responds to the message box.
vbMsgBoxHelpButton 16384 Adds Help button to the message box
VbMsgBoxSetForeground 65536 Specifies the message box window as the foreground window
vbMsgBoxRight 524288 Text is right-aligned
vbMsgBoxRtlReading 1048576 Specifies text should appear as right-to-left reading on Hebrew and Arabic systems


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.