Solved

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

Posted on 2011-03-02
5
467 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 31

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 31

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 31

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 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