• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Accessing MFC Dialog Box Controls

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Smartweb

Member
Joined
Sep 24, 2003
I have a dialog box in MFC that has a few controls on it (lables, textboxes, etc.) and I can't figure out how to access them in the code. I just want to read and write to common properties like Caption, etc., but I just can't find out how.
 
Open up MFC ClassWizard
Select "Member VariableS" tab
Select the "Control ID" for the desired control (text box, etc.)
Select Add Variable
Give it a name and type

For example for edit box with ID "IDC_DATE_DAY" a variable is created for it called m_date_day of type float.

The varible declaration is automatically put in the dialog class definition in the header file.

For example:

class CDateDlg : public CDialog
{
public:
CDateDlg(CWnd* pParent = NULL); // standard constructor
enum { IDD = IDD_DATE_DIALOG };
...
float m_date_day;
...
};

Thes variable is initialized in dialog class constructor.

CDateDlg::CDateDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDateDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDateDlg)
m_date_day = 0.0f;
//}}AFX_DATA_INIT
}

If you enter something in the edit box and want to get the value You must use this statement before doing so: UpdateData(TRUE);

For example suppose you want to read the value from the "IDC_DATE_DAY" edit box after it is entered into a variable called newday:

float newday;
UpdateData(TRUE);
newday = m_date_day;

To reverse the process you need to use this statement to force the edit box to receive a new value: UpdateData(FALSE);

For example suppose you want to write the value to the "IDC_DATE_DAY" edit box from a variable called newday:

float newday;
newday = 20.75;
m_date_day = newday;
UpdateData(FALSE);
 
Back