Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Changing a control in a function

Changing a control in a function

Changing a control in a function

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.

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.


RE: Changing a control in a function

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.

RE: Changing a control in a function

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.


RE: Changing a control in a function

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 :)


RE: Changing a control in a function

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.


RE: Changing a control in a function

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.

RE: Changing a control in a function

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.

RE: Changing a control in a function

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 :)


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close