如何按类型访问集合中的Control?

zio*_*ion 4 c# controls winforms

如何通过其类型来控制控件?

我有一个Control集合"TargetControls"

        List<Control> TargetControls = new List<Control>();
        foreach (Control page in Tabs.TabPages)
        {
            foreach (Control SubControl in page.Controls)

                TargetControls.Add(SubControl);
        }

        foreach (Control ctrl in TargetControls)...
Run Code Online (Sandbox Code Playgroud)

我需要通过其特定类型访问每个现有控件(组合框,复选框等)并访问其特定属性.我现在这样做的方式只能让我访问通用控件属性.

我不能指定类似......

Combobox current = new ComboBox ["Name"]; ///引用ComboBox'名称'的实例

然后可以访问它(已经存在的)属性进行操作?

Ami*_*shk 6

您可以使用is关键字来检查特定类型的控件.如果控件属于特定类型,请进行类型转换.

foreach (Control SubControl in page.Controls)
{
    if (SubControl is TextBox)
    {
        TextBox ctl = SubControl as TextBox;
    }
}
Run Code Online (Sandbox Code Playgroud)