MFC and namespaces

I am trying to understand how to use VC 2005 Express to create a MFC app and I am not getting a couple things about namespaces and calling forms. I do understand this is managed and not std C though. VC created a namespace for me which is the same name I gave the project.

I created a form called textBox1 in my Form1.h file, and I wish to write text to it from within a function I wrote that is not inside Main() but is in the main .cpp file. So, I added my function and did something like ..
1
2
3
4
5
6
7
8
9
using namespace my_program;

int main () {
my_function();
}

int my_function() {
Form1::textBox1->text = "this is some text";
}


The code inside my function uses the std namespace (or I should say I prepended everything with std:: since it uses some string and i/o functions).

But, I get an error about everything to the left of ->text needing to be a class/function/something. What am I not grasping here ?
closed account (z05DSL3A)
I don't think you can do MFC app's with VC 2005 Express.
Sure you can.
closed account (z05DSL3A)
If you want to do an MFC App, why are you doing managed code?

Anyway the basic code for a managed app would look somthing like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "Form1.h"

using namespace my_program;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	
	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}


It's probably because of my inability not to be a dumbass sometimes.

This is what I should have posted ..

I am using Windows Forms Application in VC 2005 Express, which I assume (probably wrongly) uses MFC ... What I don't understand is how to target form elements from a function outside of main().

Here is basically what I am doing. VC gave me the namespace based off the name of the project. I want to set the text value of a textBox form from my_function().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>

using namespace my_program;

int main () {
my_function();
}

int my_function() {
 ... do a bunch of file and string stuff ..
Form1::textBox1->text = "this is some text";
}


The reason I don't understand what's going on is because I don't understand what's going on with namespaces I think. I have to append std:: to all my string and file variables in my_function() ... Or should I switch back to using namespace std; before my_function ?

The managed code confuses me and I am probably distracted by all the pretty controls.
closed account (z05DSL3A)
The reason your code does not work is that you have not got an object of type Form1 to work with; it is not to do with namespaces.

I don’t want you to take this the wrong way but:

How much do you know about Object Oriented Programming (designing, writing and using classes)?

How much do you know about Event Driven programming?

How much do you know about graphical user interface (GUI) programming?

If the answer to these questions is not much, then I think you need to do more ground work before tackling WinForms/.net.

Broadly speaking, to do what you are tying to do you would have to derive a class from Application. Add a Form1 member to that class. Override the Run method to pass a reference to your Form1 member. You could then add a function to your derived Application class to do some work and update the form via its’ reference but you would have to find a suitable event to trigger it.
Thanks Grey Wolf.

I am just beginning OOP. I have the overall concepts down. I understand classes, structs and members etc. I am very familiar with C. I have used VB forms extensively for GUI stuff.

What I am doing now is trying to learn how to convert a C program I made into a C++ one using WFA so I can put a GUI on it. It's really just an exercise in learning. Creating the application and adding the controls is simple, now I am just trying to understand how the managed code works.

I assumed it was a namespace issue since the Form control didn't seem to be visible to the function I was trying to use on it. I have done it before, but it was just a hack and I never understood how it worked.

The Form1 member is created automatically by VC. I can put a different control on the same form and the two can 'see' each other no problem. But when I try to access Form1 from a custom function in a different source file it fails.

I guess what I don't understand is how the Form1 object relates to the rest of the program when it's added via managed code in VC Express.
closed account (z05DSL3A)
You may find the MSDN library of use:

http://msdn2.microsoft.com/en-us/library/dd30h2yb.aspx

Good luck!
Topic archived. No new replies allowed.