Solved

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

Posted on 2011-03-02
5
458 Views
Last Modified: 2013-12-14
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
Comment
Question by:shaolinfunk
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:Zoppo
ID: 35017849
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
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 35017855
Sorry, a bug in the sample code, I forgot the "-s - line 5. should be

m_strEdit1 = "2";

Open in new window

0
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 35017953
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
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018004
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
 
LVL 30

Expert Comment

by:Zoppo
ID: 35018021
Thank's - I'll do :o)
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now