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 }
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
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 }
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 }
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 }
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 }
本文转自叶子文文博客51CTO博客,原文链接http://blog.51cto.com/leafwf/185699如需转载请自行联系原作者
叶子文文