如意楼问的是webform还是winform? webform:用jQuery就能搞定: $("input[type='text']").val();得到所有的text的值去判断!winform: foreach (Control c in Page.Form.Controls) { if (c.GetType().ToString() == "TextBox") { //----------- } }
把Page.Form换成存放TextBox的panel的名字!
是winForm 在线速求解答啊~
麻烦1楼看仔细点啊,我说的是FORM下面的几个TextBox 而不是所有的
int i = 0; foreach (Control c in this.Controls) { if (c is System.Windows.Forms.TextBox) { if (c.Text.Length > 0) { i++; if (i == 2) { return true; } } } }
如何去取得Form里面的几个TextBox 然后作判断啊,请高手帮助
//假如是在提交时判断内容是否为空 private void button1_Click(object sender, EventArgs e) { //遍历窗体所有控件,如果是Text,并且为空就弹出消息框 foreach (Control c in this.Controls) { if (c is TextBox) { if (c.Text == "") { //这里可以自己去作判断写什么逻辑 MessageBox.Show("内容填写不完整!"); break; } } } }
判断name属性啊 TextBox x = c as TextBox; if(x.Name="xxxx")做什么
webform:用jQuery就能搞定:
$("input[type='text']").val();得到所有的text的值去判断!winform:
foreach (Control c in Page.Form.Controls)
{
if (c.GetType().ToString() == "TextBox")
{
//-----------
}
}
把Page.Form换成存放TextBox的panel的名字!
foreach (Control c in this.Controls)
{
if (c is System.Windows.Forms.TextBox)
{
if (c.Text.Length > 0)
{
i++;
if (i == 2)
{
return true;
}
}
}
}
//假如是在提交时判断内容是否为空
private void button1_Click(object sender, EventArgs e)
{
//遍历窗体所有控件,如果是Text,并且为空就弹出消息框
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
if (c.Text == "")
{
//这里可以自己去作判断写什么逻辑
MessageBox.Show("内容填写不完整!");
break;
}
}
}
}
TextBox x = c as TextBox;
if(x.Name="xxxx")做什么
/// <summary>
/// 验证文本框中值是否为空
/// </summary>
/// <returns></returns>
private bool Check()
{
lbMessage.Text = "";
System.Threading.Thread.Sleep(3000);
foreach (Control item in this.Panel1.Controls)
{
try
{
TextBox tb = item as TextBox;
if (tb.Text == "")
{
lbMessage.Text = "请保持数据完整性";
return false;
}
}
catch
{
} } return true;
}
那就是说全部不填写或全部填写是正确的,否则提示信息不完整所以标准答案如下: private void JudgeNull()
{
bool bNull = false;
bool bNotNull=false;
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
if (c.Text.Length > 0)
{
bNotNull = true;
}
else
{
bNull = true;
}
if (bNull && bNotNull)
{
MessageBox.Show("信息填写不完整!");
break;
}
}
}
}
{
bool bNull = false;
bool bNotNull=false;
foreach (Control c in this.Controls)
{
if (c is TextBox&&(c.Name=="textBox1"||c.Name=="textBox2"||c.Name=="textBox3"))
{
if (c.Text.Length > 0)
{
bNotNull = true;
}
else
{
bNull = true;
}
if (bNull && bNotNull)
{
MessageBox.Show("内容填写不完整!");
break;
}
}
}
}
写错了 这是webform里的 真杯具
foreach(Control tb in controls)
{
if(!string.IsNullOrEmpty(tb.Text))
{
//判断
}
}