gpt4 book ai didi

c# - 如何按类型访问集合中的控件?

转载 作者:太空狗 更新时间:2023-10-30 00:17:29 25 4
gpt4 key购买 nike

如何按类型定位控件?

我有一个控件集合“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)...

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

我不能指定类似...

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

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

最佳答案

您可以使用is 关键字来检查特定类型的控件。如果控件是特定类型,则进行类型转换。

foreach (Control SubControl in page.Controls)
{
if (SubControl is TextBox)
{
TextBox ctl = SubControl as TextBox;
}
}

关于c# - 如何按类型访问集合中的控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1568615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com