|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Creating Dialog BoxesIts time to ask the user for some feedback, and you dont want to use the Visual Basic input box because that can only accept one line of text. Besides, you dont like the way it looks (its not a great favorite among Visual Basic programmers, perhaps for that reason). Looks like youll 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.
This adds a new dialog box to the project, as shown in Figure 4.21.
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 boxs (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 programs main form, we can show that dialog box when required this waynote 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 dialogs Feedback string this way:
Private Sub Command1_Click()
Dialog.Show 1
Text1.Text = Dialog.Feedback
End Sub
And thats itnow we are supporting dialog boxes, as shown in Figure 4.22.
All About Message Boxes And Input BoxesVisual Basic provides two ways of displaying message boxes and input dialog boxes: using MsgBox() and InputBox(). Well 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. Heres 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.)
The buttons argument specifies what to put into the message box, as specified in Table 4.2. The default value for buttons is 0.
|
|
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. |