C#遍历控件的方法

首先,要想遍历,就必须找到你想找的表单里面的所有控件,然后一个个的逐一比对,当找到了你需要的控件的时候,再做你需要的操作。

1、foreach方法

foreach (Control control in 表单信息.Controls)
{
if (control is 控件类型)
{
控件类型t = (控件类型)control;
//逻辑代码
}
}

示例:

foreach (Control control in this.Controls)
{
if (control is TextBox)
{
TextBox t = (TextBox)control;
if (t.Text == "")
{
//......
}
}
}





posted @ 2011-11-28 15:17  月夜清风  阅读(11397)  评论(0编辑  收藏  举报