foreach实现

fengxingko 2009-03-28 08:14:37
/// <summary>
/// 清空界面上的按钮的内容
/// </summary>
private void ClearInputContent()
{
if (this.dateTimePicker1.Text.Length > 0)
{
this.dateTimePicker1.Text = null;
}

if (this.comboBox1.Text.Length > 0)
{
this.comboBox1.Text = null;
}
if (this.comboBox3.Text.Length > 0)
{
this.comboBox3.Text = null;
}
if (this.comboBox6.Text.Length > 0)
{
this.comboBox6.Text = null;
}
if (this.textBox1.Text.Length > 0)
{
this.textBox1.Text = null;
}
if (this.textBox2.Text.Length > 0)
{
this.textBox2.Text = null;
}
if (this.textBox3.Text.Length > 0)
{
this.textBox3.Text = null;
}
if (this.textBox4.Text.Length > 0)
{
this.textBox4.Text = null;
}
if (this.textBox5.Text.Length > 0)
{
this.textBox5.Text = null;
}
if (this.textBox6.Text.Length > 0)
{
this.textBox6.Text = null;
}
if (this.textBox7.Text.Length > 0)
{
this.textBox7.Text = null;
}
if (this.textBox8.Text.Length > 0)
{
this.textBox8.Text = null;
}
if (this.textBox9.Text.Length > 0)
{
this.textBox9.Text = null;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
this.ClearInputContent();
}

用FOREACH实现
...全文
127 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lightfox 2009-03-29
  • 打赏
  • 举报
回复
foreach (control e in TabControl1.SelectedPage.controls)
{
if (e.GetType().tostring()=="System.Windows.Forms.ComboBox")
(ComboBox)e.text=null;
if (e.GetType().tostring()=="System.Windows.Forms.TextBox")
(TextBox)e.text=null;
if (e.GetType().tostring()=="System.Windows.Forms.DateTimePicker")
(DateTimePicker)e.value="1900-01-01";
}
fengxingko 2009-03-28
  • 打赏
  • 举报
回复
补充下他们都在TabControl1里面,有更好的办法了没,写完整点
wuyq11 2009-03-28
  • 打赏
  • 举报
回复
private void test(Control c)
{
foreach (Control childControl in c.Controls)
{
if (childControl is ComboBox)
((ComboBox)childControl).Text = "";
else if (childControl is TextBox)
((TextBox)childControl).Text = "";
else if (childControl is dateTimePicker1)
{
((dateTimePicker)childControl).Format=DateTimePickerFormat.Long;
((dateTimePicker)childControl).CustomFormat=null;
}

}
bobo202020 2009-03-28
  • 打赏
  • 举报
回复
补充一点,就是最好写一个方法
然后用梯归
这样做的目的是怕你用了容器控件,适用性更强!

void Fun(Control control)
{
foreach (Control ctrl in control.Controls)
{
if (ctrl is ComboBox || ctrl is TextBox)
{
ctrl.text=null;
}
else
{
Fun(ctr);
}
}
}

然后直接调用就可以: Fun(this.Controls)
kxk19880524 2009-03-28
  • 打赏
  • 举报
回复
foreach (Control ct in this.Controls)
{
if ((ct is TextBox) || (ct is ComboBox))
{
ct.Text = "";
}
}

dateTimePicker是时间选择控件,如果清空就不满足时间格式,所以不能清空,死我活你可以设置dateTimePicker的时间
如 dateTimePicker1.Value = "2008-10-10"
ProjectDD 2009-03-28
  • 打赏
  • 举报
回复
foreach(var item in this.Controls)
{
if(item is TextBox)
if(((TextBox)item).Text.Length>0)
((TextBox)item).Text=string.Empty;
}
lightfox 2009-03-28
  • 打赏
  • 举报
回复
foreach (control e in this.controls)
{
if (e.gettype().tostring()=="System.Windows.Forms.ComboBox")
(ComboBox)e.text=null;
if (e.gettype().tostring()=="System.Windows.Forms.TextBox")
(TextBox)e.text=null;
}

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧