Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing a control in a function

Status
Not open for further replies.

asimov500

Programmer
Mar 14, 2008
21
0
0
Hi there,

I am not new to programming but I am new to C++.
I want to be able to change MFC textBox's in a function to keep my code modular.

The following line works ok in my main code.
textBox1->Text = "Not Pressed";

But when I put it in a function I get a compiler error.
eg

void laughing(){
textBox1->Text = "Not Pressed";
}

The errors are
d:\my documents\visual studio 2008\projects\suzipic\suzipic\Form1.h(164) : error C2065: 'textBox1' : undeclared identifier
d:\my documents\visual studio 2008\projects\suzipic\suzipic\Form1.h(164) : error C2227: left of '->Text' must point to class/struct/union/generic type
type is ''unknown-type''

Can anybody help with this. I have been searching for
an answer for three days now.
I can do this in Visual basic without problems.

Asimov

 
It is the scoping. Where is textBox1 declared? If it is declared in main, then only main can see it. To get everyone to see it the declaration has to be moved outside main i.e. make it global.
 
Hi xwb,

I understand that variables declared outside main are global but I didn't think this applied to textBox and such.

The textBox was drawn using the designer and it puts it where it puts it. Is there a way to change the default location in the program or do I have to manually move it outside main afterwards?

At the moment all my textBox's are in

void InitializeComponent(void)
{


I am sorry if I sound stupid but I did a lot of programming in C on the Amiga and then went to Visual Basic, so this is a steep learning curve for me.

With MFC I am not even sure where main starts. I thought it would start on Form1 eg

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
//start of program
void laughing();
button1->Text = "bert";

I have a lot to learn and can usually pick things up quite quickly, so any help would be appreciated.

Asimov
 
I have just realised I have probably said something stupid as I have just found main. It is in the cpp file. My function is in the form1.h file, and the main part of my program is in form1.h in the form1 part.

There is an #include "form1.h" in the cpp file before main. So if the buttons are declared in Form1.h then in theory they should already be outside main.

I am so confused :)


Asimov
 
Hi all,

I am learning something here. I now know I wasn't using MFC at all. Got confused there.

I am using Visual C++ express edition 2008 and I am using windows forms.

I cannot alter textBox's, use MessageBox's or anything inside a function or from when I click on a button event.

I understand about scope and that anything outside main is global. Didn't figure this applied to textBox's though. The boxes are maide in Form1.h. I mean in Visual c++/cli you use the graphical interface to make them and it puts them in for you.

I had no trouble changing textBox's in VB but in C++/cli I am having problems. I am surprised to find little information on this. I can change the textbox in main however so I know my code is correct.

Asimov
 
Hi Asimov,

A quick google search found the following site,


which gives a good tutorial for C++/CLI though not Windows Forms. I'm sure you can find a tutorial on Forms elsewhere. The introductory pages on the above site will also help you put C++/CLI and Windows Forms in perspective with earlier technologies such as C++ and MFC (still going strong).

I started out using C and Basic on an Amiga 1000. I miss it.

Hope that helps.
 
I just read a bit of the introduction to the site I gave above. It's garbage. The tutorials might be of some use but you may want to look elsewhere as well. I should have looked at it more closely.
 
Hi CooperDBM,

Strangly enough I found this forum by googling. I spent days
googling before I found this site and I also found the site you mentioned. It is quite informative in places but it doesn't have everything.

I have learn't z80 machine code on the spectrum. C, Amos Basic and Amiga Basic on the amiga 500 and 1200. And I have wrote a complete Doctor Who Database in Visual Basic. I would leave a link but I am not sure if that is allowed here. I have only been dabbling in C++/cli for a week now and it is like pulling teeth to work out some stuff.

So far I have managed to make a form which opens another form when a button is clicked, and I have dabbled with the arrays. I am having trouble sending data between forms and all that kind so stuff.

I really like to keep code as modular as possible by using Functions.

Any help would be appreciated. I have almost exhausted Google :)

Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top