Vhradice

I am new to VC++ but not to programming.

I am trying to change the Text property off a textbox when I click a button. However I cannot determine the proper syntax to reference the text field.

Form name - Form1

Textbox name - Textbox1

Button name - Button1

In Button1.Click I tried the following - all of which fail compile

Form1::Textbox1->Text = "Hello World"; /* error C2227 */

Form1::Textbox1.Text = "Hello World"; /*error C2228 */

Textbox1.Text = "Hello World"; /* error C2065 */

This can't be that hard, but I can't find where it is documented.



Re: Windows Forms Designer How do I reference a control's properties?

Ryan Lamansky

If you're inside a form event (like a button click), then something lik this should work:

this->Textbox1->Text = "Hello World";

(provided your casing is correct).

In traditional Windows.Forms programming, every form is created as an instance, and event handlers are member functions (called methods). All instance functions have a hidden "this" parameter that points to the current instance. The -> is used because both TextBox and String are reference types, accessed via pointers (they actual data is always on the heap).

Conventionally, the "this->" part is skipped, but it's useful for illustration of what's truly happening, so I always use it :)

-Ryan / Kardax





Re: Windows Forms Designer How do I reference a control's properties?

Vhrasice

I tried that and got the following errors:

error C2673: 'Go_Click' : global functions do not have 'this' pointers

error C2227: left of '->textBox1' must point to class/struct/union/generic type

error C2228: left of '.text' must have class/struct/union

Does it matter where the code is placed I placed the following in the Test.cpp file for the project:

Void Go_Click(System::Object^ sender, System::EventArgs^ e) {

this->textBox1.text="Hello World";

}

I also changed the header file as follows:

public: System::Windows::Forms::TextBox^ textBox1;





Re: Windows Forms Designer How do I reference a control's properties?

Ryan Lamansky

It absolutely matters where the code is placed :)

This "this" pointer only works in instance methods, and the code I provided will only work if that instance method is on your form class.

If you want to change the content of a text box from a global function, a reference to the form must be stored in a global location and the text box field must have public or internal access. Then you would simple replace "this" with whatever your global variable is named.

-Ryan / Kardax

PS: Global functions and variables should be avoided in any object-oriented C++ project, which includes just about anything involving .NET.





Re: Windows Forms Designer How do I reference a control's properties?

timvw

Under the assumption the Button and the TextBox are on the same form:

Void Go_Click(System::Object^ sender, System::EventArgs^ e) {
System::Form form = (System::Form) ((System::Button) sender)->Parent;
System::TextBox textBox = form->textBox1;
textBox->Text = "Hello World";
}





Re: Windows Forms Designer How do I reference a control's properties?

Vhrasice

Can you be a little more specific I created a new project called Test in Visual Studio of type CLR and template of Windows Forms Application. I put a button and a textbox on the form. In the Test.cpp file, I created a Button.Click function. This is where I tried to change the value. I have tried many different ways to address the text field, but nothing works. I moved the code to the .h file and it works as expected. This doesn't help me as I still want to know how to address conrols on a form outside the .h file. If you want, I can upload all of the code that was generated along with my attempts to reference the fields. Thank you, Vince Radice



Re: Windows Forms Designer How do I reference a control's properties?

Vhrasice

No matter where I put this code, the compile fails.



Re: Windows Forms Designer How do I reference a control's properties?

Ryan Lamansky

If you're using .Text instead of ->Text, it's guaranteed to fail because Text is a String--a reference type.

You have to place the code I provided -inside- the Form1 (or whatever you called it) class for it to work. If it's outside, it's treated as a global function and the "this" parameter won't be available.

I encourage you to get a book on C++/CLI so you can get past these stumbling blocks. With your programming background, you should be able to pick things up quickly :)

-Ryan / Kardax





Re: Windows Forms Designer How do I reference a control's properties?

Vhrasice

Thanks - I ordered a book on C++/CLI. I did notice that things worked if they were in the Form1.h file, but not in the Test1.cpp file. My view was that the header (*.h) file contained the definitions but not the actual code. I got this from some books I have on C++. It seems funny to put all of my code in the header file and not in the .cpp file. Is that the way it is with C++/CLI If that's the case, they why even have a .cpp file associated with the project Thanks for the help. Hopefully I will get past this and into the good stuff - writing code that works.