Compartilhar via


Using Common Controls in a Dialog Box

The Windows common controls can be used in dialog boxes, form views, record views, and any other window based on a dialog template. The following procedure, with minor changes, will work for forms as well.

Procedures

To use a common control in a dialog box

  1. Place the control on the dialog template using the dialog editor.

  2. Add to the dialog class a member variable that represents the control. In the Add Member Variable dialog box, check Control variable and ensure that Control is selected for the Category.

  3. If this common control is providing input to the program, declare additional member variable(s) in the dialog class to handle those input values.

    Note

    You can add these member variables using the context menu in Class View (see Adding a Member Variable).

  4. In OnInitDialog for your dialog class, set the initial conditions for the common control. Using the member variable created in the previous step, use the member functions to set initial value and other settings. See the following descriptions of the controls for details on settings.

    You can also use dialog data exchange (DDX) to initialize controls in a dialog box.

  5. In handlers for controls on the dialog box, use the member variable to manipulate the control. See the following descriptions of the controls for details on methods.

    Note

    The member variable will exist only as long as the dialog box itself exists. You will not be able to query the control for input values after the dialog box has been closed. To work with input values from a common control, override OnOK in your dialog class. In your override, query the control for input values and store those values in member variables of the dialog class.

    Note

    You can also use dialog data exchange to set or retrieve values from the controls in a dialog box.

Remarks

The addition of some common controls to a dialog box will cause the dialog box to no longer function. Refer to Adding Controls to a Dialog Causes the Dialog to No Longer Function for more information on handling this situation.

What do you want to do?

See Also

Reference

Making and Using Controls

Concepts

Controls (MFC)