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!
Who is Participating?

Improve company productivity with a Business Account.Sign Up

ZoppoConnect With a Mentor Commented:
Sorry, a bug in the sample code, I forgot the "-s - line 5. should be

m_strEdit1 = "2";

Open in new window

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,

shaolinfunkAuthor Commented:
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 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.
shaolinfunkAuthor Commented:
Here's the link to the related question if you're interested in continually helping:
Thank's - I'll do :o)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.