简体   繁体   English

如何为Winform类列表分配相同的属性?

[英]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.

相关问题 如何仅在列表中的项目的子范围上分配属性? - How do I assign a property on only a subrange of items in a list? 我如何在C#中的Winform中分配Datagridview数组 - How do I assign array of Datagridview in winform in C# 如何将.NET 4 WinForm应用程序分配给Delphi 7表单的owner属性? - How can I assign a .NET 4 WinForm application to the owner property of a Delphi 7 form? 如何将EntityCollection属性分配给另一个属性? - How do I assign EntityCollection property to another property? 如何在列表中存储具有不同泛型的相同类型的类? - How do I store the same types of classes that have a different generic inside of a list? 如何获取 .NET 中的类列表 - How do i get a list of classes in .NET 如何从过滤器或控制器为WebViewPage属性分配值? - How do I assign a value to a WebViewPage property from a filter or a controller? 如何将不同的泛型类分配给同一变量 - How to assign different generic classes to same variable 如何以编程方式分配WCF ClientCredentials ServiceCertificate属性? - How do I programatically assign WCF ClientCredentials ServiceCertificate property? 如何使用Tab在winform属性网格的属性之间移动 - How do I use Tab to move between properties of winform property grid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM