遍历控件
Code
1 //清除文本框内容
2 private void btclear_Click(object sender, EventArgs e)
3 {
4 //先将窗体上所有的控件遍历出来。
5 foreach (Control ctr in this.Controls)
6 {
7 if (ctr is GroupBox )//判断是否是groupbox
8 {
9 foreach (Control ctr1 in ctr.Controls)//如果是继续遍历groupbox中的控件
10 {
11 if (ctr1 is TextBox )//判断是否是TextBox
12 ctr1.Text = string.Empty;//如果是清空其文本
13 //ctr1.Text ="";//两种方法均可
14 }
15 }
16 }
17 }
1 //清除文本框内容
2 private void btclear_Click(object sender, EventArgs e)
3 {
4 //先将窗体上所有的控件遍历出来。
5 foreach (Control ctr in this.Controls)
6 {
7 if (ctr is GroupBox )//判断是否是groupbox
8 {
9 foreach (Control ctr1 in ctr.Controls)//如果是继续遍历groupbox中的控件
10 {
11 if (ctr1 is TextBox )//判断是否是TextBox
12 ctr1.Text = string.Empty;//如果是清空其文本
13 //ctr1.Text ="";//两种方法均可
14 }
15 }
16 }
17 }
Code
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 foreach (Control ctl in Page.Controls[0].Controls)
4 {
5 if (ctl.GetType().Name == "TextBox")
6 {
7 TextBox tb = new TextBox();
8 tb = (TextBox)this.FindControl(ctl.ID);
9 tb.Text = string.Empty;
10 }
11 }
12 }
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 foreach (Control ctl in Page.Controls[0].Controls)
4 {
5 if (ctl.GetType().Name == "TextBox")
6 {
7 TextBox tb = new TextBox();
8 tb = (TextBox)this.FindControl(ctl.ID);
9 tb.Text = string.Empty;
10 }
11 }
12 }
批量赋值label文本
1 private void timer1_Tick(object sender, EventArgs e)
2 {
3 Random dm = new Random();
4 foreach (Label s in this.Controls)
5 {
6 //判断label控件的名字除这三种外,label的文本为1-35的随机数
7 if (!s.Name.Equals ("label1")&& !s.Name.Equals ("label2")&&s.Name !="exit")
8 s.Text = dm.Next(1, 36).ToString();
9 }
10 }
1 private void timer1_Tick(object sender, EventArgs e)
2 {
3 Random dm = new Random();
4 foreach (Label s in this.Controls)
5 {
6 //判断label控件的名字除这三种外,label的文本为1-35的随机数
7 if (!s.Name.Equals ("label1")&& !s.Name.Equals ("label2")&&s.Name !="exit")
8 s.Text = dm.Next(1, 36).ToString();
9 }
10 }
利用递归改变窗体中listview的子控件颜色
1 private void 背景颜色紫ToolStripMenuItem_Click(object sender, EventArgs e)
2 {
3 fillcolor(this ,Color .Purple);
4 }
5
6 void fillcolor(Control a,Color b )
7 {
8 for (int i = 0; i < a.Controls.Count; i++)
9 {
10 if(!a.Controls [i].Equals(listView1))
11 a.Controls[i].BackColor =b;
12 fillcolor(a.Controls[i],b);
13 }
14 }
1 private void 背景颜色紫ToolStripMenuItem_Click(object sender, EventArgs e)
2 {
3 fillcolor(this ,Color .Purple);
4 }
5
6 void fillcolor(Control a,Color b )
7 {
8 for (int i = 0; i < a.Controls.Count; i++)
9 {
10 if(!a.Controls [i].Equals(listView1))
11 a.Controls[i].BackColor =b;
12 fillcolor(a.Controls[i],b);
13 }
14 }
遍历页面的RadioButtonList,更改选中方式
1 for (int i = 0; i < this.Controls.Count; i++)
2 {
3 for (int j = 0; j < this.Controls[i].Controls.Count; j++)
4 {
5 if (this.Controls[i].Controls[j] is RadioButtonList)
6 {
7 ((RadioButtonList)this.Controls[i].Controls [j]).SelectedIndex = -1;
8
9 }
10 }
11
12 }
13
1 for (int i = 0; i < this.Controls.Count; i++)
2 {
3 for (int j = 0; j < this.Controls[i].Controls.Count; j++)
4 {
5 if (this.Controls[i].Controls[j] is RadioButtonList)
6 {
7 ((RadioButtonList)this.Controls[i].Controls [j]).SelectedIndex = -1;
8
9 }
10 }
11
12 }
13