日期:2014-05-20  浏览次数:20733 次

请问有3个textbox,如何判断其中哪个为空???
比方说,我在一个form上有3个textbox和1个button,我需要对textbox里面的内容进行限制,比方说不能为空。在我按下这个button的时候,如果其中某一个textbox的内容为空,就让这个控件获得焦点。

可能说的不太清楚,大概就是这个意思,谢谢!!!

------解决方案--------------------
这个好说,用一个循环就可以了:
foreach (Control con in this.Controls)
{
TextBox _tb = con as TextBox;
if (_tb != null && _tb.Text.Trim()==string.Empty)
{
_tb.Focus();
break;
}
}

------解决方案--------------------
foreach( Control c in this.Controls )
{
if( c is TextBox )
//your work
}

------解决方案--------------------
被hbxtlhx(平民百姓)搶先了。
------解决方案--------------------
你们真快. 顶一个,接分.正确答案已经有了,我就不多说了.
------解决方案--------------------
if
------解决方案--------------------
foreach(System.Windows.Forms.Control ct in this.Controls)
{
if(ct.GetType() == typeof(System.Windows.Forms.TextBox))
{
if(ct.Text.Trim().Length ==0)
{
ct.Focus();
return;
}

}
}
------解决方案--------------------
你的界面上的textbox可能有多个空的,你准备把焦点给哪个?
------解决方案--------------------
if(text1.text.length==0)
------解决方案--------------------
你的10几个textbox是不是都放在form,还是放在form的panel上,
foreach(System.Windows.Forms.Control ct in this.Controls)这个this这里指的是容纳textbox的容器,你的“对象引用设置到对象的实例”应该是这个ct为被定义~~~
{
if(ct.GetType() == typeof(System.Windows.Forms.TextBox))
{
if(ct.Text.Trim().Length ==0)
{
ct.Focus();
return;
}
}
}

------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
foreach (Control tmp in this.Controls)
{
TextBox textbox = tmp is TextBox ? (TextBox)tmp : null;
if (textbox != null & tmp.Text == " ") tmp.Focus();
}
}
给分!
------解决方案--------------------
用JS来判断嘛。最容易了。
------解决方案--------------------
up
------解决方案--------------------
楼主这个问题还没有搞定啊....
哪里出错了!
------解决方案--------------------
你的窗体里都有什么样的控件呢?
是不是只有一个Button和三个TextBox呢?
------解决方案--------------------
用的是Vs2005还是VS2003?
------解决方案--------------------
private void button1_Click(object sender, EventArgs e)
{
foreach (Control tmp in this.groupBox1.Controls)
{
if (tmp is ComboBox)
{
ComboBox cb = (ComboBox)tmp;
if (cb.Text.Trim() == string.Empty)
{
cb.Focus();
break;
}
}
}
}

------解决方案--------------------