Ross Sheppard High School
Computer Programming 20
Student Learning Guide
The Dialog Box Program


  When you have finished this project, you will be able to:  
use a MessageBox  
use an If . . . Then  

describe the following terms:

  • MessageBox
  • DialogBox
 
  1. Create a new folder called Dialog Box on your personal drive.

  2. The illustration below shows the visual implementation of the Dialog Box Program.



    The Dialog Box Program

    When the user presses any of the buttons, btnYesNo, for example, a meesage "you clicked the YesNo button will appear in the TextBox txtUserChoice. A Dialog Box will also appear giving the user buttons containing the choices listed on the btnYesNo button. When the CommandButton btnExit is pressed the program will create a dialog box asking the user "Are you sure you want to quit?" Depending on the user's choice, the program will return the user to it or end.
  3. Start a new Standard.exe project in Visual Basic.

  4. Use the table below for the visual implementation of the Dialog Box program:

    Object

    Property

    Setting

    Form Name frmDialogBox
      Caption The Dialog Box Program
      Background Color Cyan
      Startup Position Screen Center
    TextBox Name txtUserChoice
      Text Make it blank
      Forecolor
    (Font Color)
    Red
      Font Ms Sanserif 12
    CommandButton Name btnYesNo
      Caption YesNo
    CommandButton Name btnOKCancel
      Caption OKCancel
    CommandButton Name btnOK
      Caption OK
    CommandButton Name btnRetryCancel
      Caption RetryCancel
    CommandButton Name btnExit
      Caption Exit
      Font MS SansSerif Bold 14
  5. In the code view of your program, write the pseudo-code for the Dialog Box program as comments within the code. In other words, the comments should decribe in easy-to-understand language, the purpose of each segment of code in each sub-routine.

  6. Save the Dialog Box project as prjDialogBox and the form as frmDialogBox. Add the comments and Option Explicit statement to the General_Declarations() area of your form:



  7. Write the code for the CommandButton btnExit. Save your project, then run it. Try the Exit button.

  8. Add the following code for the CommandButton btnYesNo:

    'The next line places the message in the
    'TextBox txtUserChoice>

    txtUserChoice.Text = "You clicked the YesNo Button"

  9. Save your project and try the code for the btnYesNo. What does this code instruct Visual Basic to do?

  10. Type similar code for all of the other buttons. Try each one. Save your project.

  11. When you were trying the various buttons, what did you notice about the text displayed in the TextBox txtUserChoice?

  12. So far, you have programmed the buttons to display a message in the TextBox called txtUserChoice.

  13. The next step is to type the code that will display the pop-up Dialog Box with the proper button, choices, messages and icons.

  14. Visual Basic has a number of standard Dialog Boxes available to the programmer. The job of the programmer is to specify the buttons, text, icons and actions required.

  15. The syntax required for a Dialog Box is:

    Response = MsgBox("You pressed the YesNo button", vbQuestion + vbYesNo, "The YesNo Dialog Box")

  16. Enter the following code in the CommandButton btnYesNo_Click() Procedure, just below the code that you have already typed:

    'The next line displays the YesNo dialog box

    Response = MsgBox("You pressed the YesNo button",_
    vbQuestion + vbYesNo, "The YesNo Dialog Box")

    Note: the above code starting with Response should be on a single line. When you see code with the _ character, type it on a single line. The _ indicates that the code could not fit on one line in the example above.

  17. Your CommandButton btnYesNo code should now look like the illustration shown below:



  18. Save your project and run the program. When you click the CommandButton btnYesNo, the dialog box should appear. If not, check your code. What happens when you click one of the buttons in the Dialog Box? Why?.

  19. Create appropriate code for each of the other CommandButtons, with the exception of the Exit button.

  20. Once the code is entered for each CommandButton, save your project and run it. Try each of the buttons. You should get a message in the TextBox txtMessage as well as the dialog box for each button you press.

  21. Now it is time to type the code required for each of the choices that a user can make when a button is pressed in the Dialog Box.

  22. The code that you write for each CommandButton will instruction Visual Basic: "If the user presses the button, then do this." This is called an If . . . Then statement. In the View Code window, select the btnYesNo_Click() Procedure.

  23. Directly underneath the Private Sub btnYesNo_Click() statement, add the following code:

    'The following code defines the variable called Response
    'which is the selection the user makes on the Dialog Box

    Dim Response As Integer

  24. Directly above the End Sub statement for this CommandButton type the following code:

    'The next lines use the user's choice to display a message in the
    'TextBox txtUserChoice based on the button clicked

    If Response = vbYes Then

    txtUserChoice.Text = "You chose the Yes Button"

    Else

    txtUserChoice.Text = "You chose the No Button"

    End If

    Save your project and try both buttons.

  25. Just below the programmer information in your code, add a comment that explains the purpose of the Response variable.

  26. The syntax for an If . . . Then statement is:

    If this event happens Then

    Do this procedure

    Else

    Do a different procedure

    End If

    You should read the above statement "If the user makes this selection, then perform this procedure; otherwise perform a different procedure."

  27. Visual Basic provides several different icons for the Dialog Box:

    Summary of Visual Basic Icon Constants

    Constant Name Icon Displayed Sample of icon
    vbCritical Critical Icon

    vbQuestion Question Icon

    vbExclamation Warning Message Icon

    vbInformation Information Icon

  28. Add the appropriate code to each of the CommandButtons, using a different Constant (Icon) for each. Do not add code to the the Quit button.

  29. For the Exit button, create a Dialog Box that asks the user, "Are you sure you want to quit?" and the choices Yes and No. Use the Question Icon. The title of the Dialog box should be "Quit Dialog Box Program?"

    Summary of VB Dialog Boxes

    Constant Name Button Displayed
    vbOKOnly OK
    vbOKCancel OK and Cancel
    vbAbortRetryIgnore Abort, Retry, and Ignore
    vbYesNoCancel Yes, No, and Cancel
    vbYesNo Yes and No
    vbRetryCancel Retry and Cancel
  30. Save your project and run it. Try each of the buttons to ensure they work.

  31. Click here to see the marking guide for this project.

  32. When you have finished, send an email to your teacher asking that this project be marked.


© 1998-2000 N.F. Mathew, EdD
Edited J. Heslinga
File name: Dialog.htm
Last updated on February 14, 2001