I am using MFC visual studio 2013, i have a form with two edit boxes, X and Y . I want the sum X and Y to display in static text box on the form when I click DISPLAY BUTTON.
X + Y = Z
X edit box1 Y edit box2 Z static text box, to display result Z
I am using MFC visual studio 2013, i have a form with two edit boxes, X and Y . I want the sum X and Y to display in static text box on the form when I click DISPLAY BUTTON.
X + Y = Z
X edit box1 Y edit box2 Z static text box, to display result Z
On
Landstalker has given a good start of an answer. I'd differ from his solution in one crucial respect though.
Rather than using GetDlgItem to play with the value directly, I'd add a variable for the static control (the one you've given the ID IDC_STATIC_RESULT). It'll be just about like the m_x and m_y you've already created, but obviously with some other name (e.g., m_z).
Then comes a crucial point. When you associate value type variables with edit/static controls (or others, but those are what we care about right now), MFC uses a function named UpdateData to move data between the control and the associated variable. You call UpdateData(); or UpdateData(true);, to copy data from the control into the associated variable. You call UpdateData(false); to copy data from the variable into the control.
In your button handler you'll want to do that, so it'll come out something like this:
void CaddDlg::OnBnClickedButton1()
{
UpdateData(); // get the data from the controls
m_z = m_x + m_y; // compute the new value
UpdateData(false); // push the updated data back to the controls
}
That works well enough in this case, but with a complex dialog, the UpdateData stuff can be pretty old in a hurry, especially when you have controls that interact with each other. In such a case, it's generally better to ignore the value variables, and just use control variables, which represent the control itself, so you can read and write controls individually (whereas UpdateData always copies values from all controls into associated variables, or vice versa).
In header (.h)
Add this function in your header : This function will be called when you click
In your CPP (.cpp)
Add listener
This part was generated automatically for you when you have done "Add Variable"
Calcul Function