Type-Safe Access to Controls With ClassWizard

OverviewHow Do I

If you are familiar with DDX features, you can use the Control property in ClassWizard to create type-safe access.  This approach is easier than creating controls without ClassWizard.

If you simply want access to a control’s value, DDX provides it. If you want to do more than access a control’s value, use ClassWizard to add a member variable of the appropriate class to your dialog class. Attach this member variable to the Control property.

Member variables can have a Control property instead of a Value property. The Value property refers to the type of data returned from the control, such as CString or int. The Control property enables direct access to the control through a data member whose type is one of the control classes in MFC, such as CButton or CEdit.

Note   For a given control, you can, if you wish, have multiple member variables with the Value property and at most one member variable with the Control property. You can have only one MFC object mapped to a control because multiple objects attached to a control, or any other window, would lead to an ambiguity in the message map.

You can use this object to call any member functions for the control object. Such calls affect the control in the dialog box. For example, for a check-box control represented by a variable m_checkboxDefault, of type CButton, you could call:

m_checkboxDefault.SetState(TRUE);

Here the member variable m_checkboxDefault serves the same purpose as the member function GetMyCheckbox shown in Type-Safe Access to Controls Without ClassWizard. If the check box is not an auto check box, you would still need a handler in your dialog class for the BN_CLICKED control-notification message when the button is clicked.

For more information about controls, see Controls.

See Also   Life Cycle of a Dialog Box, Type-Safe Access to Controls Without ClassWizard