Solved

Can someone show me how to display "Hi!"?

Posted on 2011-02-11
26
458 Views
Last Modified: 2013-12-14
I've created a Dialog-based MFC application called "Example" in Visual Studio 2008.  In Source Files folder there's an Example.cpp and ExampleDlg.cpp (with corresponding .h files in Header Files folder).

I've also got a simple button called IDC_BUTTON1 and an "edit control" text box called IDC_EDIT2 (with variable 'm_edit1' attached to it).  When I run the code below...pushing the button shows "Hi!" in the text box.

All of this code is contained in ExampleDlg.cpp.  My question is...how do I move the code to Example.cpp and still have everything work properly?  That is, when I push the button, "Hi!" will display in the text box?
void CExampleDlg::OnBnClickedButton1()
{	
        m_edit1 = _T("Hello Earth");
	UpdateData(FALSE);
)

Open in new window

0
Comment
Question by:shaolinfunk
  • 14
  • 5
  • 5
  • +1
26 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34873865
I wouldn't recommend that.  The code for manipulating the controls on the dialog should stay within the class for the dialog.  Moving it elsewhere just complicates things excessively.
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 34873910
I agree with AndyAinscow. Why are you trying to move the function?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34873934
Ok, here's what I REALLY want to do.  I would like to run a function..say a math function..that adds 1+1...and then spits out the result ("2") to the dialog...instead of just saying "Hi!"

I'm new to this programming thing...and it didn't feel right to have all of the math code in the ExampleDlg.cpp.  I thought it would be better to have all of the calculations done in Example.cpp.

Can you show me how to do this?
0
 
LVL 7

Expert Comment

by:JimBeveridge
ID: 34874254
If you are just trying to understand all of this, then get it working in ExampleDlg.cpp first, then "factor" the code to elsewhere.

Example.cpp contains the application object. The application object takes care of services that are relevant to the application as a whole, like the name of the application, registry read/write, and lots of other stuff.

ExampleDlg.cpp contains the code for viewing the dialog. In a theoretically perfect world, it only contains code that determines how to view the data - fonts, windows, stuff like that.

So your math code really "shouldn't be" in either of those files, because your math code is really its own object, such as "Account" or even "Calculator". So your math code should go in its own file in its own object. Since you are learning, it's easier to put everything in ExampleDlg.cpp until you understand all of the code.

When you are ready to take the next step, you refactor by putting all of the math code in its own object, with its own .h and .cpp files.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34874524
Thanks Jim, for the logical suggestion and explanation.  I will do as you say.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34874525
You would have a function something like the following:
double AddTwoNumbers(double x, double y)
{
  return x+y;
}

Now you could have this a member of the dialog class, the application class or (as JimBeveridge said) a completely new file (class?) for handling your maths functions so it could be shared with other dialogs for instance.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34874969
Hi Andy,

Yes, that's what I would like to do.  I've created that function...but I don't want to put it in ExampleDlg.cpp or Example.cpp.  I'd like to put it in a "Functions.cpp".  Can you show me how to do that in such a way that pushing my dialog button will still yield the result of "x+y"?

I guess my first question is...how do I create a new .cpp file in my Example project?  I'm guessing you can all see just how new of a beginner I am.
0
 
LVL 32

Accepted Solution

by:
sarabande earned 250 total points
ID: 34876301
you can create a new cpp file by right-clicking on the 'Source Files' folder of the project tree in Solution Tab and choose the Add -> New Item ... C++ File (.cpp) and set functions.cpp for name and finally hit Add button.

same way but for Header Files folder you should create a functions.h.

then add

#ifdef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED

class MathFunc
{
public:
     static int add(int operand1, int operand2);
};

#endif

in functions.cpp you do

#include "functions.h"

int MathFunc::add(int operand1, int operand2)
{
    return operand1 + operand2;
}


then add two edit fields x (IDC_EDIT_X) and y(IDC_EDIT_Y)  to your form and static field called result (IDC_RESULT for id).

add member variables m_x, m_y, m_result of type int for all three fields.

then on button click get data from screen by call

    UpdateData(TRUE);

then call

   m_result = MathFunc::add(m_x, m_y);

and

   UpdateData(FALSE);

to push result on screen.

Sara

0
 
LVL 32

Expert Comment

by:sarabande
ID: 34876310
i forgot to mention that in exampledlg.cpp you need to include functions.h (below include of stdafx.h)

Sara
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34876328
in c++ it is good style to have classes and not global functions.

a static member function nevertheless is global function that means you can it call without an object of the class simply by having MathFunc:: as prefix.

Sara
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34877970
HI Sarah,

Thanks for the detailed step by step instructions.  I am stuck at this step:

"add member variables m_x, m_y, m_result of type int for all three fields."

I am adding member variables but I don't know how to choose "type int".  As you can see in the snapshot "CEdit" is actually the only type available in the dropdown menu.
Noname.jpg
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34878397
unselect the ccontrol checkbox
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34879400
Thank you Andy, I was able to proceed and set up everything correctly.  Now,  I ran into some errors that I have no idea why they occur.  Please see snapshot...I suspect it's because that in my Functions.h....lines 2 - 9 are grayed out.  But, I have no idea why they are grayed out!  Can someone please explain?
Noname.jpg
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 34879681
I think the first line should start with (note the n in the middle)
#ifndef
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34879735
Yes! That fixed the error and everything works!  Thank you EE folks so much for helping out a beginner.  I split the points 50/50 between Andy and Sara.

Can someone explain to me why it seems that the preferred way of naming variables is to use "m_" in front?  ie "m_x, m_y, etc.?"

Also, let's say I want to rename the variable from "m_x" to "m_Input1"..how do I do this?  Actually I think I'll post this in another question so you can legitimately get more points.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34879746
Here's link to another 500 point question..the same question I posed above:

http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_26817346.html
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34879755
Posting another 500 points question related to this thread:

http://www.experts-exchange.com/Programming/Editors_IDEs/C_CPP_CS/Visual_CPP/Q_26817349.html

I simply ask:

What is the point of IFNDEF?  

And FUNCTIONS_H_INCLUDED?

And defineFUNCTIONS_H_INCLUDED?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34880743
Hi Sara or Andy...

The program above compiles without any errors but when I actually ran it I didn't get any results.  I typed the number "1" in the first box, "2" in the second box, and then hit the button that runs the program but the sum "3" was not displayed.

So I debugged and stepped through the code line by line.  After the UpdateData(TRUE) line I noticed that the value of variable m_x and m_y were still "0" in the locals watch menu....where it should have been "1" and "2", the numbers I manually typed in....then m_result = 0 in the next line of code.

how do I get the code to properly read the numbers that i inputted into the edit control boxes?  UpdateData(TRUE) didn't seem to have its intended effect..
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34880890
The UpdateData(TRUE) causes the function CExampleDlg::DoDataExchange to be called.

if you add a varriable like the m_x or m_y by using the wizard ('Add Variable ...') , the wizard would add the m_x to the DoDataExchange. so you should find a line in the DoDataExchange where the m_x was handled:

      DDX_Text(pDX, IDC_EDIT2, m_x);

that statement would do the exchange from screen to member or vice versa.

if you don't have such a statement or if the control id is not right or if m_x is not an int member in exampledlg.h the UpdateData(TRUE); would not work.

note you also may not have a UpdateData(FALSE); statement before.

Sara



0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34880907
I zipped up the entire project because I'm pretty sure I followed everything presented here in the thread....I followed Andy's earlier suggestion to UNCHECK the control variable box...but then I googled it and the control variable box has something to do with enabling DoDataExchange..did I disable DoDataExchange?

I searched for DDX_Text but got no results....
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34880925
here's the zip file of entire project except the ILK, NCB, SUO, OBJ files since EE doesn't allow those to be uploaded for some reason.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34880948
PCH, DEP, IDB, APS, RC, PROJ files also taken out since EE doesn't allow them
Example.zip
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34881133
1) i searched entire solution for DDX....nothing like your code snippet comes up
2) the controls' IDs are IDC_EDIT1, IDC_EDIT2, and IDC_RESULT (and IDC_BUTTON1)
3) m_x is an int member in ExampleDlg.h under public:
4) UpdateData(FALSE) is stated at the very end....is that OK?
0
 
LVL 32

Expert Comment

by:sarabande
ID: 34881777
it is all ok with your source beside that the entry in DoDataExchange was missing.

so remove the m_x, m_y, m_result from your header and from constructor CExampleDlg::CExampleDlg and go back to resource editor. do the following for each IDC_EDIT1, IDC_EDIT2, IDC_RESULT:

  - right-click on control and choose 'Add Variable'
  - change category from 'Control' to 'Value'
    (means you want a member variable for value and not a variable for control)
 - choose 'int' type for 'Variable Type'
 - give name m_x, m_y and m_result
 - finish step with button 'Finish'

now look in exampledlg.cpp and function DoDataExchange.

Sara
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 34882254
You might also want to look at the following (amongst other things) in the help files.
GetDlgItemText
GetWindowText
atol
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 34882376
Aha!  That did the trick sara...The key was..when adding a variable I must change category from 'Control' to 'Value' in order to make "int" available in the dropdown menu.  Then, it will create the DDX line you mention.

Earlier, I was simply unselecting the 'Control Checkbox', which while it made "int" available in the dropdown menu, and allowed the program to compile without errors, did not create a line for DoDataExchange.

Anyways, thank you for catching that small detail...because that has made all the difference! And I never would have caught that on my own.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

744 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

9 Experts available now in Live!

Get 1:1 Help Now