[英]How do I assign the same property to a list of winform classes?
I have an arraylist of all a bunch of winform controls (textboxes, checkboxes, radio buttons, etc). 我有所有winform控件(文本框,复选框,单选按钮等)的arraylist。
I am trying to enable/disable them all at once. 我正在尝试一次启用/禁用它们。
Code is as below: 代码如下:
Button button = new Button();
Label label = new Label();
Listview listview = new ListView();
ArrayList controlList = new ArrayList();
controlList.add(button);
controlList.add(label);
controlList.add(listview);
foreach(object o in controlList) {
o.enabled = false;
}
But of course, this wouldn't work, since the class object does not have the property "enabled" in it. 但是当然,这是行不通的,因为类对象中没有“启用”属性。
What should I do to make this work? 我应该怎么做才能使这项工作?
You should use a List<Control>
. 您应该使用
List<Control>
。 This is the base class of all System.Windows.Controls
. 这是所有
System.Windows.Controls
的基类。 You can iterate over all properties of the Control type: 您可以遍历Control类型的所有属性:
Button button = new Button();
Label label = new Label();
Listview listview = new ListView();
var controlList = new List<Control>();
controlList.Add(button);
controlList.Add(label);
controlList.Add(listview);
foreach(var o in controlList) {
o.Enabled = false;
}
Change: 更改:
o.enabled = false;
To 至
(o as Control.enabled = false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.