2012-11-22 38 views
2

嘿所以我有下面的代码应该抛出错误,如果文本框是空的,但它不只是继续与它会做什么,他们不是,并添加一个项目0或其他任何东西,而不是我的代码有问题吗?尝试捕获验证空文本框

private void BtnAdd_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      theVisit.name = txtName.Text; 
      theVisit.address = txtAddress.Text; 
      theVisit.arrival = DateTime.Parse(txtArrival.Text); 
      //Update theVisit object to reflect any changes made by the user 

      this.Hide(); 
      //Hide the form 
     } 
     catch (Exception) 
     { 
      if (txtName.Text == "") 
       MessageBox.Show("please enter a customer name"); 

      if(txtAddress.Text == "") 
       MessageBox.Show("Please enter a customer address"); 

      if(txtArrival.Text == "") 
       MessageBox.Show("Please enter an arrival time"); 
     } 

if (txtName.Text == "" || txtAddress.Text == "" || txtArrival.Text == "") 
      MessageBox.Show(" Please enter a value into all boxes"); 
     else 
     theVisit.name = txtName.Text; 
     theVisit.address = txtAddress.Text; 
     theVisit.arrival = DateTime.Parse(txtArrival.Text); 
     //Update theVisit object to reflect any changes made by the user 
+0

那里可能抛出的唯一事情是DateTime.Parse()。空文本框上的'.Text'不会引发异常,它只是返回一个空字符串。使用'string.IsNullOrEmpty(txtName.Text)'检查空值。 – drch

+0

您的theVisit对象的setter是否会抛出异常?你能否显示你的theVisit对象的类实现? –

+0

theVisit是一个列表,但我想我通过把它放在try not catch部分 – TAM

回答

4

在try-catch语句用于捕获并处理异常。如果索引超出范围,设置为null的变量的成员被访问,并且在许多其他情况下,则可以抛出异常。空着的TextBox本身不是错误,并且不会引发异常。

我建议你使用完全不同的方法。将ErrorProvider添加到您的表单中。您可以在“组件”部分的工具箱中找到它。现在,您可以将下面的代码添加到您的形式:

private HashSet<Control> errorControls = new HashSet<Control>(); 

private void ValidateTextBox(object sender, EventArgs e) 
{ 
    var textBox = sender as TextBox; 
    if (textBox.Text == "") { 
     errorProvider1.SetError(textBox, (string)textBox.Tag); 
     errorControls.Add(textBox); 
    } else { 
     errorProvider1.SetError(textBox, null); 
     errorControls.Remove(textBox); 
    } 
    btnAdd.Enabled = errorControls.Count == 0; 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    txtName.Tag = "Please enter a customer name"; 
    txtAddress.Tag = "Please enter a customer address"; 
    errorProvider1.BlinkStyle = ErrorBlinkStyle.NeverBlink; 

    ValidateTextBox(txtName, EventArgs.Empty); 
    ValidateTextBox(txtAddress, EventArgs.Empty); 
} 

选择ValidateTextBox方法作为TextChanged事件的所有您的文本框的错误处理程序。将所需的消息插入文本框的Tag属性中。将ErrorProviderBlinkStyle属性设置为ErrorBlinkStyle.NeverBlink。您可以在代码或表单设计器中执行这些设置。

现在红色错误符号将出现在空白文本框旁边。如果将鼠标悬停在它们上面,将出现带有错误消息的工具提示。


UPDATE

我更新了上面的代码自动禁用或启用 “添加” 按钮上。因此我添加了一个HashSet,其中包含当前处于错误状态的所有控件。如果该设置为空,则该按钮被启用,否则被禁用。

+0

嘿,这是好的,但如果文本框是空的,红色的符号在那里,我仍然按添加,它仍然将它添加到列表 – TAM

+0

是否有避免这种情况?只有在所有文本框中都有内容时才会添加它 – TAM

+0

我添加了自动启用或禁用按钮的代码。请注意,对于相同的值,可以调用'Add'和'Remove'方法多次,而不会产生错误。 –

2

你应该总是避免尝试捕捉如果可能的话,因为性能命中见下面的例子:

 //set name 
     if(string.IsNullOrEmpty(txtName.Text)) MessageBox.Show("please enter a customer name"); 
     else theVisit.name = txtName.Text; 

     //set address 
     if(string.IsNullOrEmpty(txtAddress.Text)) MessageBox.Show("please enter a customer address"); 
     else theVisit.address = txtAddress.Text; 

     //set arrival time 
     if(string.IsNullOrEmpty(txtArrival.Text)) MessageBox.Show("please enter an arrival time"); 
     else { 

      DateTime dt = default(DateTime); 
      bool successParse = DateTime.TryParse(txtArrival.Text, out dt); 

      if(!successParse) MessageBox.Show("please enter a valid arrival time"); 
      else theVisit.arrival = dt; 

     } 
+1

谢谢工作更容易 – TAM

+0

@TAM没问题。不太清楚为什么我会因此而失望? –