Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

How do I display the results of 1+1 in my MFC app dialog?

I would ultimately like to create a simple dialog-based app that does a simple calculation, like adding 1+1, and  displays the result in a text box.  The caveat is, that I would like the calculation performed in a separate .cpp file (Functions.cpp) so that I can isolate the calculation part of the program from everything else.  So, I did the following:

1.   Created an MFC dialog-based application in VS2008 called "FirstApp".  The files FirstApp.cpp and FirstAppDlg.cpp were created (along with their respective header files).  

2.  Using the Resource View tab, I inserted a button dialog called IDC_BUTTON1.  This is the button to be pushed.

3.  Then I Inserted an edit control dialog called IDC_EDIT1. This is where the results of 1+1 is to be displayed.

4. I created Functions.cpp and Functions.h (no code in there yet)

5. Double clicked on the button dialog and my cursor is now blinking under "void CFirstAppDlg::OnBnClickedButton1()"

....and now I'm stuck.  I've drawn a blank and don't know what to do from here.

Can someone help me along towards my ultimate goal?  Thank you very much!
Avatar of Zoppo
Zoppo
Flag of Germany image

Hi shaolinfunk,

do you want to re-use the edit control in a way that i.e. enter '1+1' there and after pressing the button the result '2' should be printed in there?

Easiest to handle edit controls is to add a CString member variable and associate it with the control - you can do this in the resource editor like this:

- Right click the edit control and select 'Add Variable ...'
- In the upcoming dialog select 'Value' in 'Category', 'CString' in 'Variable type' and enter a 'Variable name' (i.e. 'm_strEdit1')
- Click 'Finish'

Now your dialog class has a new member 'm_strEdit1' which can be used to read/write the content of the edit control.

In your function 'void CFirstAppDlg::OnBnClickedButton1()' you can now read the entered text, modify it and write it back like this:

void CFirstAppDlg::OnBnClickedButton1()
{
 UpdateData( TRUE ); // get text from edit control into 'm_strEdit1'
 // modify 'm_strEdit1' here just for testing
 m_strEdit1 = 2;
 UpdateData( FALSE ); // put the text from 'm_strEdit1' to the edit control
}

Open in new window


BTW: The harder part will be to implement 'Functions.*' to calculate any kind of functions you need ...

Hope that helps,

ZOPPO
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shaolinfunk

ASKER

Got it Zoppo.  That worked and I see what you've done here.

I get that UpdateData reads what is inputted into the edit control.....
And then we've just assigned the value of 2 to the variable we've created...and UpdateData(False) makes it show that value on the dialog...

This is a good first step and I get what's going on.  I will ask post another question for the 2nd step....how do we make it so that when I click on the button it calculates "1+1" in Functions.cpp and displays the result in the edit control box.
Thank's - I'll do :o)