C++/CLI:在运行时更改 属性 动态添加的控件

C++/CLI: Change Property of a dynamically added control at Runtime

我看到它可以与 C# 一起使用,但不能在 Visual C++ 2015 中使用

System::Windows::Forms::Label^ mylabel= (gcnew System::Windows::Forms::Label());
mylabel->Name = L"pole";
mylabel->Text = "Hello";
this->Controls->Add(mylabel);

注意这里mylabel是一个临时变量。 现在代码适用于 C#

Control cc = this.Controls.Find("pole", true).First();
cc.text="New";

我试过了,因为没有 .First() 或 ->first(),

Control^ x = this->Controls->Find(L"pole", true);

肯定会显示错误

`cli::array<System::Windows::Forms::Control ^, 1> ^" cannot be used to initialize an entity of type "System::Windows::Forms::Control ^`"

如何在运行时将该对象作为控件获取?

查找方法returns一个数组。在您的 C# 示例中,您调用 First() ,其中 returns 是数组中的第一项(返回对控件的引用)。在 C++ 示例中,您不调用 First() 或执行任何操作来检索单个项目。这就是为什么错误消息表明您无法将数组(注意 cli::array 错误)转换为控件引用。