如何在Form的代码之外引用文本Label?

时间:2010-02-19 04:56:41

标签: 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";

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

1 个答案:

答案 0 :(得分:0)

您可以做的一件事是打破封装,通常是一件坏事,并将TextLabel1设置为公开。

因此

private: System::Windows::Forms:Label^ TextLabel1;

变为

public: System::Windows::Forms:Label^ TextLabel1;

现在您可以从调用代码中更改TextLabel1:

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