遍历窗体上所有控件(遍历子控件)

1:

//遍历窗体上所有CheckBox控件 .net winform

StringBuilder sb = new StringBuilder();
foreach (Control c in Controls)
{
    if (c is CheckBox)
    {
        if (((CheckBox)c).Checked == true)
        {
            sb.Append(",");
            sb.Append(c.Text.ToString());
        }
    }
}
sb.Append(",");

textBox1.Text = sb.ToString();

2:

asp.net 遍历TextBox控件使用递归来做这个才能实现

private void findText(ControlCollection c)
    {
        foreach (Control item in c)
        {
          
            if(item.HasControls())
            {
                findText(item.Controls);
            }
            if (item is TextBox)
            {
                ((TextBox)(item)).Text = "aa";
            }
          
        }
    }

调用findText(this.Controls);

3:

递归的妙用—遍历子控件

我们在ASP.NET编程中, 经常需要遍历一个Web控件的子控件 ,找到所需的控件并获取控件中相应的值。以前我都是采用循环的方式遍历子控件,但当子控件是复杂的树形结构,比如:子控件也有子控件,子控件的子控件也有子控件。这时如果用循环的方式,就要用嵌套循环,而有时我们很难确定我们所要找的控件在子控件树的哪一层,昨天我就为些付出了代价,因为一个控件在内部增加了Panel控件,并将它的子控件移到了Panel控件上,我通过循环怎么也找不到所需的控件。
     既然子控件表现为一个树形结构,为什么我不用递归去遍历子控件?当我看着不太优雅的嵌套循环代码时,我突然这样想到。使用递归,根本不用关心所需的控件在哪一层,而且代码简洁。
     下面就是两种遍历方式:
1、循环方式:
for (int i =0; i<GlobalCategoryPanel.Controls.Count;i++)//GlobalCategoryPanel是个Panel控件
            {
                if(GlobalCategoryPanel.Controls[i] is AdvancedPanel)//AdvancedPanel是个自定义控件
                {
                    AdvancedPanel ap=(AdvancedPanel)GlobalCategoryPanel.Controls[i];
                    for(int ap_i=0;ap_i<ap.Controls.Count;ap_i++)
                    {
                        if(ap.Controls[ap_i] is Panel)
                        {
                            Panel ap_panel=(Panel)ap.Controls[ap_i];
                            for(int ap_panel_i=0;ap_panel_i<ap_panel.Controls.Count;ap_panel_i++)
                            {
                                if(ap_panel.Controls[ap_panel_i] is RadioButtonList)
                                {
                                    RadioButtonList rbl=(RadioButtonList)ap_panel.Controls[ap_panel_i];
                                    al.Add(int.Parse(rbl.SelectedValue));
                                }
                                               
                            }
                        }
                    }
                }
            }
2、递归方式
private void Button1_Click(object sender, System.EventArgs e)
        {
            FindSelecedControl(GlobalCategoryPanel);
        }
        private void FindSelecedControl(Control control)//递归函数
        {
            if(control is RadioButtonList)
            {
                RadioButtonList rbl=(RadioButtonList)control;
                al.Add(int.Parse(rbl.SelectedValue));
            }
            else
            {
                for(int i=0;i<control.Controls.Count;i++)
                {
                    FindSelecedControl(control.Controls[i]);
                }
            }
        }
4:

遍历Panel 控件里面的button

foreach (Control c in this.Controls)
            {
                Panel p = c as Panel;
                if (p != null)
                {
                    foreach (Control cc in p.Controls)
                    {
                        Button c_btn = cc as Button;
                        if (c_btn != null)
                        {
                            if (c_btn.Name == "5")
                            {
                               c_btn.Click+=new EventHandler(c_btn_Click);
                               // MessageBox.Show("Lbtn5");
                                break;
                            }
                        }

                    }
                }
            }

 foreach (System.Windows.Forms.Control control in this.groupBox2.Controls)//遍历groupBox2上的所有控件
   {
    if (control is System.Windows.Forms.PictureBox)
    {
     System.Windows.Forms.PictureBox pb = (System.Windows.Forms.PictureBox)control;
     pb.AllowDrop = true;
    }
   }

foreach (System.Windows.Forms.Control control in this.Controls)//遍历Form上的所有控件
   {
    if (control is System.Windows.Forms.PictureBox)
    {
     System.Windows.Forms.PictureBox pb = (System.Windows.Forms.PictureBox)control;
     pb.AllowDrop = true;
    }
   }

阅读更多
个人分类: WinForm学习
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭