C# WinForm遍历窗体控件的3种方法
作者:五维思考 时间:2022-02-01 16:58:17
1.循环遍历
private void GetControls(Control fatherControl)
{
Control.ControlCollection sonControls = fatherControl.Controls;
foreach (Control control in sonControls)
{
listBox1.Items.Add(control.Name);
}
}
结果:能获取到Panel、GroupBox、TabControl等控件
问题:Panel等控件上面的子控件获取不到
2.递归遍历
private void GetControls(Control fatherControl)
{
Control.ControlCollection sonControls = fatherControl.Controls;
foreach (Control control in sonControls)
{
listBox1.Items.Add(control.Name);
if (control.Controls != null)
{
GetControls(control);
}
}
}
结果:能获取到绝大多数控件
问题:Timer、ContextMenuStrip等控件获取不到
3.使用反射
private void GetControls(Control fatherControl)
{
System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance);
for (int i = 0; i < fieldInfo.Length; i++)
{
listBox1.Items.Add(fieldInfo[i].Name);
}
}
结果:所有控件都被获取到了
DevExpress
控件无法使用this.Controls
进行遍历,只能通过反射的方法获得,如下代码:
public void SearchBarManager()
{
Type FormType = this.GetType();
FieldInfo[] fi = FormType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
foreach (FieldInfo info in fi)
{
if (info.FieldType == typeof(DevExpress.XtraBars.BarManager))
{
DevExpress.XtraBars.BarManager bar = (info.GetValue(this)) as DevExpress.XtraBars.BarManager;
foreach (DevExpress.XtraBars.BarItem bi in bar.Items)
{
MessageBox.Show(bi.Name);
}
}
}
}
来源:https://www.cnblogs.com/zhaoshujie/p/14638460.html
标签:C#,WinForm,遍历,窗体控件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#中使用DevExpress中的ChartControl实现极坐标图的案例详解
2022-12-05 06:30:39
![](https://img.aspxhome.com/file/2023/2/104232_0s.png)
C#不提升自己程序的权限实现操作注册表
2023-01-24 18:51:31
![](https://img.aspxhome.com/file/2023/0/82150_0s.png)
Java泛型机制与反射原理相关知识总结
2023-11-11 06:02:15
![](https://img.aspxhome.com/file/2023/4/58914_0s.jpg)
详解Java的回调机制
2023-07-27 07:17:43
![](https://img.aspxhome.com/file/2023/7/60657_0s.png)
基于java中cookie和session的比较
2021-08-17 00:49:44
IntelliJ IDEA 2019.1.1 for MAC 下载和注册码激活教程图解
2023-07-23 08:01:50
![](https://img.aspxhome.com/file/2023/8/62648_0s.jpg)
Android中Snackbar的使用方法及小技巧
2023-10-30 03:13:31
![](https://img.aspxhome.com/file/2023/8/103888_0s.gif)
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决
2022-06-14 23:21:51
java报错:找不到或无法加载主类的解决方法简单粗暴
2023-03-29 17:20:58
![](https://img.aspxhome.com/file/2023/4/93074_0s.png)
C++右值引用与move和forward函数的使用详解
2023-07-05 19:27:33
Java项目开发中实现分页的三种方式总结
2021-08-25 21:36:36
java中常见的死锁以及解决方法代码
2023-04-07 19:47:30
![](https://img.aspxhome.com/file/2023/2/64422_0s.png)
java图搜索算法之DFS与BFS详解
2022-08-15 15:48:57
![](https://img.aspxhome.com/file/2023/6/63026_0s.png)
java集合中list的用法代码示例
2023-04-11 23:29:17
Java Fluent Mybatis 分页查询与sql日志输出详解流程篇
2023-01-06 05:35:43
![](https://img.aspxhome.com/file/2023/3/96103_0s.png)
C#实现排列组合算法完整实例
2023-04-16 09:36:59
SpringSecurity解决POST方式下CSRF问题
2023-07-18 18:59:51
SpringBoot整合screw实现数据库文档自动生成的示例代码
2023-11-29 05:30:15
![](https://img.aspxhome.com/file/2023/9/60579_0s.png)
Java中的显示锁ReentrantLock使用与原理详解
2021-11-14 07:04:00
![](https://img.aspxhome.com/file/2023/4/69694_0s.jpg)
Linux下g++编译与使用静态库和动态库的方法
2023-06-21 13:41:46
![](https://img.aspxhome.com/file/2023/2/68232_0s.jpg)