c# 遍历子控件,比如Form下的group,或者panel 方法

方法很好用.目的是遍历所有容器的子控件… 方法1
private void GetControl(Control.ControlCollection ctc, ref int checkNull)
{
foreach (Control ct in ctc)
{
if (ct is TextBox)
{
if (ct.Text.Length <= 0)
{
checkNull = 1;
}
}

//C#只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.HasChildren)
{
GetControl(ct.Controls,ref checkNull);
}
}
}
调用的时候直接 这样用 ==========c# 遍历子控件,比如Form下的group,或者panel
int check = 0;
GetControl(this.groupBox1.Controls, ref check);
if (check != 1)
{
运行…..
}
else
{
MessageBox.Show(“请输入必要参数”);
}
==============================方法2 ,不过功能没上面的好用

//遍历控件
public void OperateControls(Control control)
{
foreach (Control c in control.Controls)
{
if (c is Panel)
{
OperateControls(c);
}
if (c is GroupBox)
{
OperateControls(c);
}
if (c is TextBox)
{
// 它是 TextBox, 要干什么随便你
if (c.Text == “”)
{
MessageBox.Show(“必填参数不能为空!”);
return;
}
}
}
}
===========c# 遍历子控件,比如Form下的group,或者panel===== 下面是参考资料
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
GetControl(Controls);
}
private void GetControl(Control.ControlCollection ctc)
{
foreach (Control ct in ctc)
{
//调用AddControlInofToListBox方法获取控件信息
AddControlInofToListBox(ct);
//C#只遍历窗体的子控件,不遍历孙控件
//当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
if (ct.HasChildren)
{
GetControl(ct.Controls);
}
}
} private void AddControlInofToListBox(Control ct)
{
switch (ct.GetType().Name)
{
//如果是ListBox、CheckBox、Button
case “ListBox”:
case “GroupBox”:
case “Button”:
listBox1.Items.Add(“控件名:” + ct.Name);
break;
//如果是CheckBox
case “CheckBox”:
if (((CheckBox)ct).Checked)
{
listBox1.Items.Add(“控件名:” + ct.Name + “,是否选中:是”);
//((CheckBox)ct).Checked = false;
}
else
{
listBox1.Items.Add(“控件名:” + ct.Name + “,是否选中:否”);
//((CheckBox)ct).Checked = true;
}
break;
//如果是RadioButton
case “RadioButton”:
RadioButton rdb = (RadioButton)ct;
if (rdb.Checked)
{
listBox1.Items.Add(“控件名:” + ct.Name + “,是否选中:是”);
//rdb.Checked = false;
}
else
{
listBox1.Items.Add(“控件名:” + ct.Name + “,是否选中:否”);
//rdb.Checked = true;
}
break;
//其它值
default:
listBox1.Items.Add(“控件名:” + ct.Name + “,值:” + ct.Text);
break;
}
}

//遍历groupBox1控件的子控件 private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
//可以共用上面的,遍历窗体控件的方法
//只是参数由Controls改为groupBox1.Controls
GetControl(groupBox1.Controls);
}
————————————-
foreach(TextBox t in this.Controls)
{
MessageBox.Show(t.Text);
}
for(int i=0;this.Controls.Count-1;i++)
if(this.Controls[i] is TextBox)
this.Controls[i].Text=””;
/// <summary>
/// 设置此界面中控件的某些属性
/// </summary>
/// <param name=”ctl”></param>
public void Set_Controls(Control ctl)
{
//当控件没有子控件时
if ( !ctl.HasChildren)
{
switch(ctl.GetType().ToString())
{
case “System.Windows.Forms.Label”:
break;
case “System.Windows.Forms.Button”:
break;
case “System.Windows.Forms.TextBox”:
break;
case “System.Windows.Forms.ListView”:
break;
case “System.Windows.Forms.GroupBox”:
break;
case “System.Windows.Forms.ComboBox”:
break;
case “System.Windows.Forms.ImageList”:
break;
case “System.Windows.Forms.DataGrid”:
break;
case “System.Windows.Forms.MainMenu”:
break;
case “System.Windows.Forms.TreeView”:
break;
}
}
else //当控件有子控件时
{
int i = 0;
while ( i < ctl.Controls.Count )
{
Set_Controls( ctl.Controls[i] );
i ++;
}
}
}

你调用时,可以这样用:
Set_Controls(this);
private void button1_Click(object sender, System.EventArgs e)
{
foreach(TextBox s in this.Controls )
{
MessageBox.Show (s.ToString());


}
}
foreach(Control c in Controls)
{
if(c is TextBox)
MessageBox.Show(“good”);
}
===============

分类:

发表评论