在VC2005中如何通过name查找特定控件。

来源:百度知道 编辑:UC知道 时间:2024/05/29 16:47:36
在一个项目中我创建了若干个TextBox。这些TextBox的作用类似,系统将它们命名成了不同的名字,如:TextBox1,TextBox2等等。

现在我在一个函数中想根据参数访问其中的某一个。例如:得到参数为1我访问TextBox1;参数为2,我访问TextBox2;以此类推。

如果数量很大,或者控件的总数是动态生成的话,使用switch case结构就非常的不方便。有没有办法让我能通过参数直接访问对应的控件?

例如类似下面这种格式:
Somefunction(index)->Text = "SomeThing";
可是这些控件都是VC2005自动生成的,我应该如何把他们放到数组里呢?
我尝试过声明数组:
private: System::Windows::Forms::TextBox^ allTextBox[16];
但是编译报错:
error C2728: 'System::Windows::Forms::TextBox ^' : a native array cannot contain this managed type
1> Did you mean 'array<System::Windows::Forms::TextBox ^>'?

error C4368: cannot define 'allTextBox' as a member of managed 'TongJi::Form1': mixed types are not supported

这个问题比较棘手一些,不过可以给你一个思路:你可以将textbox1.text="1";textbox2.text="2" 依次类推,完了,获取所有的textbox放到数组里面,判断你获取的index 如果是那个textbox的text属性是index.toString(),就操作.呵呵,应该可以实现.