2011-08-24 71 views
0

我有一个按钮和一个文本框,我想检查文本框验证是否正确,并且当我点击按钮时正确传递。否则用户应该收到警告,再次检查他在文本框中写入的内容。使用按钮验证文本框?

+1

其中的代码你迄今为止写的吗? – Waqas

回答

1
public bool Validations() 
    { 
     if (string.IsNullOrEmpty(Textbox.Text)) 
     { 
      MessageBox.Show(" Error "); 
      return false; 
     } 
     else 
     { 
      // your code 
     } 
    } 

私人无效的button1_Click(对象发件人,EventArgs的)

{

 if (!Validations()) 
      return; 

    } 
+0

在方法中使用textBox并不是首选,也许可以将textBox或TextBox.text作为输入参数或TextBox的引用。 – Burimi

0

尝试使用ErrorProvider。它可能有帮助。

private void button1_Click(object sender, EventArgs e) 
{ 
    // check if something is wrong 
    errorProvider1.SetError(textBox1,"Some Error!"); 
} 
相关问题