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.
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. This adds a new dialog box to the project. 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. 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.
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. 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. 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.
0 comments:
Post a Comment
Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.