Formatting Functions     Formatting Date and Time     Procedures

Dialog Boxes

Dialog Boxes are used to get information from the user or simply to display a message. There are several kinds of dialog boxes, but only VB's built-in InputBox and MsgBox will be discussed on this page.

MsgBox Statement

This is the simplest use of the MsgBox which just displays a message to the user.

MsgBox "Hello" 
Text1.Text = "Bill Gates"
Msgbox Text1.Text & " is not a valid name" 

Either of the above MsgBox statements present dialog boxes (which are actually just small forms) with an OK button. By default the name of your project will be in the title bar. The second example uses the ampersand to concatenate a message from a TextBox and a literal string. You can change the appearance of the MsgBox with some optional arguments.

MsgBox "Hello", vbExclamation, "Greeting" 

The second argument is a VB built-in constant which will add an exclamation icon to the MsgBox. The third argument is the string you want displayed in the title bar. Note the different sound you'll get with the exclamation icon.
There are four VB constants that you can use for the icon argument.

  • vbCritical (warns the user of a serious problem)
  • vbExlamation (a non-critical warning message)
  • vbQuestion (use when the MsgBox is asking a question)
  • vbInformation (simple informative messages)

MsgBox Function

You can ask the user a question and display various buttons in the MsgBox Function, which has the same syntax as the MsgBox statement, but the arguments are enclosed in quotes and the Function needs a place (variable or property of a control) to store the VB constant which corresponds to which of the buttons was clicked.

Dim iResponse As Integer 
iResponse% = MsgBox("Are you over 18?", vbQuestion + vbYesNo, "Verify Age")

Note the constant vbYesNo, (which displays Yes and No buttons) is added to the icon constant. Adding constants creates a unique value which the MsgBox interprets. The iResponse variable holds a value (again, a VB constant, there's no need to memorize the actual values) which you can use in code to perform the next task based on what button the user clicked.

if iResponse% = vbYes Then
     MsgBox "You are allowed to vote", vbInformation, "Election"
Else
     MsgBox "Sorry, you're underage!", vbExclamation, "Election"
End If

Here are the constants for the different button combinations available.

  • vbOkOnly
  • vbOkCancel
  • vbRetryCancel
  • vbYesNo
  • vbYesNoCancel
  • vbAbortRetryIgnore

InputBox Function

If you need some text from the user, rather than simply a yes or no question, use the InputBox. Like the MsgBox Function, the InputBox needs a place to store the user's input, in this case a string.

Dim sResponse As String 
sResponse$ = InputBox("What city do you live in?", , "San Diego", 500, 500)

Let's look at the InputBox arguments. The first is the same as the MsgBox, the prompt. The second is the title, but in the above example, we're electing to use the default by not specifying anything. If you decide against using one a function's optional arguments, you must still use the comma separator. The third argument, the default text, is also optional. The last two numbers are X and Y (Left and Top) values, which you can use if you want to place the InputBox in a certain position on the screen. (The MsgBox does not have this capability.)

The InputBox is displayed with OK and Cancel buttons, you can't change that. If the user clicks Cancel, an empty string is returned. If the user clears the InputBox and clicks OK, this also returns an empty string. You cannot differentiate between these two user actions which produce the same result. If this is a problem for your situation, you will have to create your own custom dialog box. Create a Form with the BorderStyle property set to 3 - Fixed Dialog and draw any controls which are needed. Show the Form modally and obtain the user's input from properties of the dialog's controls.

Formatting Functions     Formatting Date and Time     Procedures

Online Casino
Neteller

Copyright © 2005, 2006 Joren NetSoft
All rights reserved.