Teach Yourself Visual C++ 5 in 21 Days

Chapter 6: Dialog Boxes


Chapter 6 shows you how to invoke another standard Windows feature: the dialog box. You learn how to add standard and custom dialog boxes to your project. The objectives important to this chapter are:

  • adding a predefined dialog box to a project
  • adding a custom dialog box to a project
  • using the return value of a dialog box

Although it seems longer, the first part of this chapter is quite repetitive, so you can save time with the copy and paste features in the coding section. An observation may be appropriate: when doing the copy and paste procedure, you must know what to copy and what to paste.

A few students may need to review basic concepts about functions. You must understand that a function is called by some event in a Windows program. Note that the names of functions in this project (and all others) should contain a logical reminder of the event calling the function. The name alone does not determine the link, it is determined when you are in the Code Wizard screen and set up the link to the event/message map. If you were coding by hand, and not using the wizard, you would have to pay much more attention to the names used by Windows to refer to these events.

A dialog box is a device to get input back from a user. You pop a dialog box on the screen when you want the user to make a choice about what to do next. You have already used the basic function for this in previous programs: the MessageBox() function. You may recall that the default behavior of a message box is to present only an OK button to the user. This is fine, as long as you only wanted to send a message to the user and get an acknowledgement. Chapter 6 shows you how to ask for a choice from the user as well.

The first project in the chapter starts on page 166. You create a five button Dialog Box application. Don't confuse the project dialog box with the four subsequent dialog boxes that you teach the application to create.

It may be helpful to work in pairs, sharing books, so that you can look at the picture of the application design on page 166, while working on the object creation and property settings on pages 168 and 169.

On page 169, you create a variable and associate it with the Edit Box in your project. This control does not need to be an edit box, since the user will not type in it, but it is visually appealing to use one. You will use the variable associated with the Edit Box to change the contents of the box, making use of the UpdateData() function as in previous projects.

You attach code to the exit button on page 170 (which you should be able to do in your sleep by now). The next button to be coded is on page170 as well, and it establishes the pattern for the rest. When you have this code right, remember to copy, paste and modify it for the next three buttons to save typing.

You begin by creating a function to be called when the button you are coding is clicked. The code is shown on page 171. Several things happen in it:

  • You create a variable to use in this function. This variable is to receive the return value of the MessageBox() function, which you call next. Note: If you copy the code from this button click function to another button click function, the variable name does not need to change. It is an automatic (local) variable, known only to the function it is declared in.
  • The MessageBox() function is called. Note that you can pass three parameters to it, specifying:
    1. The string you want displayed as the message to the user.
    2. The string you want displayed in the title bar of the message box.
    3. The constants that make this a dialog box and determine what buttons and icon the user sees in it.
  • A value is returned to the calling code when the user clicks a button in the message box. The value is caught in the variable you made for that purpose.
  • A series of if conditions are tested. You should test for the value each possible button in the message box could produce.
  • Depending on which test is passed, the variable associated with the Edit Box in your application is given a new value and it's value is sent to the screen with the UpdateData(FALSE) command.

This sequence is repeated for each button you attach code to. The differences are in the buttons and icon you are told to use for each one, showing you the variety available, and the tests associated with those buttons.

The next section of the chapter tells you about creating a custom dialog box, as seen on page 181. This is a common approach for asking the user specific questions, yet offering a more varied response than just clicking a button.

Begin this project as usual, following the directions on pages 182 and 183. There are only a few new ideas in this project:

  • you will have a button on the first screen that pops up another screen to collect data
  • you will attach a variable to this second screen, read a value into it, and display it on a third screen
  • you will add a class and edit a header file

It should be clear to you what is happening from the text descriptions, but you will be required to discuss the procedure in class, so become familiar with it.