如何遍历WinForm中自定义控件内的组件
展开全部
一个递归方法取得页面上所有类型为textbox的控件,并对其清空,相信你看了后有所感悟。
private void Button1_Click(object sender, System.EventArgs e)
{
foreach (Control ctl in this.Controls)
{
this.txtClear(ctl);
}
}
private void txtClear(Control ctls)
{
if(ctls.HasControls())
{
foreach (Control ctl in ctls.Controls)
{
txtClear(ctl);
}
}
else
{
if (ctls.GetType().Name == "TextBox")
{
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctls.ID);
tb.Text = "";
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
foreach (Control ctl in this.Controls)
{
this.txtClear(ctl);
}
}
private void txtClear(Control ctls)
{
if(ctls.HasControls())
{
foreach (Control ctl in ctls.Controls)
{
txtClear(ctl);
}
}
else
{
if (ctls.GetType().Name == "TextBox")
{
TextBox tb = new TextBox();
tb = (TextBox)this.FindControl(ctls.ID);
tb.Text = "";
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询