Visual studio 2008 如何在表单';s代码?

Visual studio 2008 如何在表单';s代码?,visual-studio-2008,forms,visual-c++,Visual Studio 2008,Forms,Visual C++,代码示例1 //Set Label. this->TextLabel1= (gcnew System::Windows::Forms::Label()); 代码示例2 //When Button Clicked.... TextLabel1->Text = "Button has been pressed"; 如何在表单代码之外引用(+更改)已设置的标签。另一个cpp或头文件?您可以做的一件事是破坏封装,这通常是一件坏事,并将TextLabel1设置为public 因此 变成 p

代码示例1

//Set Label.
this->TextLabel1= (gcnew System::Windows::Forms::Label());
代码示例2

//When Button Clicked....
TextLabel1->Text = "Button has been pressed";

如何在表单代码之外引用(+更改)已设置的标签。另一个cpp或头文件?

您可以做的一件事是破坏封装,这通常是一件坏事,并将TextLabel1设置为public

因此

变成

public: System::Windows::Forms:Label^ TextLabel1;
现在您可以从调用代码更改TextLabel1:

Form1 ^form = gcnew Form1();
form->TextLabel1->Text = "Text has been changed from the outside.";
Application::Run(form);
Form1 ^form = gcnew Form1();
form->TextLabel1->Text = "Text has been changed from the outside.";
Application::Run(form);