Winform traversing the window all the controls (in several ways) C # traversing all the controls form or type all controls

 

This link: https://blog.csdn.net/u014453443/article/details/85088733

Buttoned technology exchange group: 460 189 483

C # traversing all the controls form or type all controls

  1.  
    // traverse the form all the controls,
  2.  
    foreach (Control control in this.Controls)
  3.  
    {
  4.  
    // traverse operation after ...
  5.  
    control.Enabled = false;
  6.  
    }
  1.  
    Through all the controls in a panel
  2.  
     
  3.  
    foreach (Control control in this.panel4.Controls)
  4.  
    {
  5.  
    control.Enabled = false;
  6.  
    }
  1.  
    Through all the TextBox controls or all types DateTimePicker control
  2.  
     
  3.  
    Copy the code
  4.  
    foreach (Control control in this.Controls)
  5.  
    {
  6.  
       // loop through all TextBox ...
  7.  
    if (control is TextBox)
  8.  
    {
  9.  
    TextBox t = (TextBox)control;
  10.  
    t.Enabled = false;
  11.  
    }
  12.  
       // loop through all DateTimePicker ...
  13.  
    if (control is DateTimePicker)
  14.  
    {
  15.  
    DateTimePicker d = (DateTimePicker)control;
  16.  
    d.Enabled = false;
  17.  
    }
  18.  
    }

Bowen mainly controls the following figures to compare the way these two methods to obtain control:

1. The easiest way:

  1.  
    private void GetControls1(Control fatherControl)
  2.  
    {
  3.  
    Control.ControlCollection sonControls = fatherControl.Controls;
  4.  
    // loop through all the controls
  5.  
    foreach (Control control in sonControls)
  6.  
    {
  7.  
    listBox1.Items.Add(control.Name);
  8.  
    }
  9.  
    }

 result:

 

The results are shown in ListBox acquired the right, you can be found not acquired child control Panel, GroupBox, TabControl like control.

2. Increase in the original recursive way:

  1.  
    private void GetControls1(Control fatherControl)
  2.  
    {
  3.  
    Control.ControlCollection sonControls = fatherControl.Controls;
  4.  
    // loop through all the controls
  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.  
    }

result:

 

The vast majority of controls are to be acquired, but there are still two controls Timer, ContextMenuStrip not get to.

3. Use reflection (the Reflection)

  1.  
    private void GetControls2(Control fatherControl)
  2.  
    {
  3.  
    //reflection
  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.  
    }

结果:

 

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

 

Guess you like

Origin www.cnblogs.com/wfy680/p/12002269.html