[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

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!
0
shaolinfunk
Asked:
shaolinfunk
  • 3
  • 2
1 Solution
 
ZoppoCommented:
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
0
 
ZoppoCommented:
Sorry, a bug in the sample code, I forgot the "-s - line 5. should be

m_strEdit1 = "2";

Open in new window

0
 
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 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.
0
 
shaolinfunkAuthor Commented:
Here's the link to the related question if you're interested in continually helping:

http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_26858308.html
0
 
ZoppoCommented:
Thank's - I'll do :o)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now