Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

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

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

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
I agree with AndyAinscow. Why are you trying to move the function?
Avatar of shaolinfunk

ASKER

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?
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.
Thanks Jim, for the logical suggestion and explanation.  I will do as you say.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i forgot to mention that in exampledlg.cpp you need to include functions.h (below include of stdafx.h)

Sara
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
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
unselect the ccontrol checkbox
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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Posting another 500 points question related to this thread:

https://www.experts-exchange.com/questions/26817349/What-does-IFNDEF-do-and-why-do-I-care-to-use-it-Please-explain.html

I simply ask:

What is the point of IFNDEF?  

And FUNCTIONS_H_INCLUDED?

And defineFUNCTIONS_H_INCLUDED?
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..
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



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....
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.
PCH, DEP, IDB, APS, RC, PROJ files also taken out since EE doesn't allow them
Example.zip
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?
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
You might also want to look at the following (amongst other things) in the help files.
GetDlgItemText
GetWindowText
atol
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.