Winform遍历窗口的所有控件(几种方式实现)

摘要:
=空){GetControls1;}}结果:已获得大多数控件,但仍有两个控件Timer和ContextMenuStrip尚未获得。

扣扣技术交流群:460189483

C#遍历窗体所有控件或某类型所有控件

  1. //遍历窗体所有控件,
  2. foreach (Control control in this.Controls)
  3. {
  4. //遍历后的操作...
  5. control.Enabled = false;
  6. }
  1. 遍历某个panel的所有控件
  2. foreach (Control control in this.panel4.Controls)
  3. {
  4. control.Enabled = false;
  5. }
  1. 遍历所有TextBox类型控件或者所有DateTimePicker控件
  2. 复制代码
  3. foreach (Control control in this.Controls)
  4. {
  5. //遍历所有TextBox...
  6. if (control is TextBox)
  7. {
  8. TextBox t = (TextBox)control;
  9. t.Enabled = false;
  10. }
  11. //遍历所有DateTimePicker...
  12. if (control is DateTimePicker)
  13. {
  14. DateTimePicker d = (DateTimePicker)control;
  15. d.Enabled = false;
  16. }
  17. }

博文主要以下图中的控件来比较这两种方式获取控件的方式:Winform遍历窗口的所有控件(几种方式实现)第1张

1. 最简单的方式:

  1. private void GetControls1(Control fatherControl)
  2. {
  3. Control.ControlCollection sonControls = fatherControl.Controls;
  4. //遍历所有控件
  5. foreach (Control control in sonControls)
  6. {
  7. listBox1.Items.Add(control.Name);
  8. }
  9. }

结果:

Winform遍历窗口的所有控件(几种方式实现)第2张

获取的结果显示在右侧的ListBox中,可以发现没有获取到Panel、GroupBox、TabControl等控件中的子控件。

2. 在原有方式上增加递归:

  1. private void GetControls1(Control fatherControl)
  2. {
  3. Control.ControlCollection sonControls = fatherControl.Controls;
  4. //遍历所有控件
  5. foreach (Control control in sonControls)
  6. {
  7. listBox1.Items.Add(control.Name);
  8. if (control.Controls != null)
  9. {
  10. GetControls1(control);
  11. }
  12. }
  13. }

结果:

Winform遍历窗口的所有控件(几种方式实现)第3张

绝大多数控件都被获取到了,但是仍然有两个控件Timer、ContextMenuStrip没有被获取到。

3. 使用反射(Reflection)

  1. private void GetControls2(Control fatherControl)
  2. {
  3. //反射
  4. System.Reflection.FieldInfo[] fieldInfo = this.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
  5. for (int i = 0; i < fieldInfo.Length; i++)
  6. {
  7. listBox1.Items.Add(fieldInfo[i].Name);
  8. }
  9. }

结果:

Winform遍历窗口的所有控件(几种方式实现)第4张

结果显示所有控件都被获取到了。

免责声明:文章转载自《Winform遍历窗口的所有控件(几种方式实现)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇动态创建FastreportArcMap和ArcGIS Pro加载百度地图下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WinForm下窗体权限设计

权限设计 笔者不才看了园子里面很多园友写关于权限设计这块内容,那么笔者也在添一笔。这个是笔者在上完软件工程课程后,上交的一篇笔者论文,这里分享给大家交流,当然笔者经验尚浅,若内容有误,请大家指点出来,若大家有什么更好的想法,请提出来共同学习。 一.引言 在软件开发中, 从操作系统到一个仅仅能够发布文章的网站,都要涉及到权限的管理。在Windows 操作...

如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮

原文:http://msdn.microsoft.com/en-us/library/ms171619(v=vs.85).ASPX public class DataGridViewDisableButtonColumn : DataGridViewButtonColumn { public DataGridViewDisableB...

Winform开发框架之对话框样式同化

早在《Winform分页控件重大更新,并实现普通版、DotNetBar、DevExpress三大版本整合更新(附各种例子源码)》随笔中,就集成了三种界面样式的分页控件(普通版、基于DotNetBar样式和基于DevExpress样式三种),后来也就形成了我三种不同的Winform开发框架界面样式。 1)基于传统经典模式的界面,采用了OutLookBar工具...

C#WinForm,TCP调试工具

Server端配置 参考代码 1)设置基本参数 /// <summary> ///以本机作测试 /// </summary> private IPAddress serverIP = IPAddress.Parse("192.168.0.105"); /// <summary> ///完整终端地址 ///...

c#Winform程序的toolStripButton自己定义背景应用演示样例源代码

C# Winform程序的toolStrip中toolStripButton的背景是蓝色的,怎样改变背景及边框的颜色和样式呢? 实现此功能须要重写toolStripButton的Paint方法 这里仅仅是给出解决这个问题的思路和方法,例如以下图,当鼠标移到button上,背景会变为黑色 实现代码例如以下: ToolStripBu...

Winform开发框架之单据窗体生成(主从表,流水单号)

项目源码下载地址:https://github.com/GarsonZhang/GZFramework.Demo 前言 1.在开始本节前请先重置代码为 chapter-03-start 懒人地址:https://github.com/GarsonZhang/GZFramework.ShareDemo/tree/chapter-03-start 2.创建表...