0
votes

Since I am a total beginner in MFC I need a help to understand some basics. At the moment it is not clear to me how can I grab some value (by ID or something else) and use it, or change it...

Lets say I have something like this:

enter image description here

The ID of edit boxes are: IDC_EDIT1, IDC_EDIT2, IDC_EDIT3 (respectively).

The ID of Calculate button is IDC_BUTTON1.

How can I grab the value of IDC_EDIT1, and add it to IDC_EDIT2 and then show it IDC_EDIT3 on Calculate click?

After creating this Dialog, I get the following code:

void CMFCApplication1Dlg::OnBnClickedButton1()
{

}


void CMFCApplication1Dlg::OnEnChangeEdit1()
{

}


void CMFCApplication1Dlg::OnEnChangeEdit2()
{

}


void CMFCApplication1Dlg::OnEnChangeEdit3()
{

}
2
You need to obtain the Edits values, convert to numbers, calculate, convert the result back to (MFC specific?) string and then set the third Edit value. - Ron
Dialog Data Exchange explains, how to hook up instance variables to controls, and have them reflect the current state. - IInspectable

2 Answers

3
votes

For starter, try this:

void CMFCApplication1Dlg::OnBnClickedButton1()
{
   int a = GetDlgItemInt(IDC_EDIT1);
   int b = GetDlgItemInt(IDC_EDIT2);
   SetDlgItemInt(IDC_EDIT3, a+b);
}
1
votes

An MFC CDialog is a CWnd (inheritance). So you can access its childs with GetDlgItem. If you only want to process integers, you can even use the helper method GetDlgItemInt which will give you the text of the CEdit as an integer. Once this is done, you just add the two numbers and use the result to set the value of the last CEdit (that should be either inactive of read-only) with SetDlgItemInt.

If you want to accept floating point, you should read the values with SetDlgItemText and write them with SetDlgItemText and process the conversions to/from double by hand.

What I mean is that you will not have to use the OnEnChangeEditx notifications but do all the stuff in the OnBnClickedButton1 one.

As you have not shown your current code, I cannot say more here...