3

嘿,所以我有以下代码,如果文本框为空,它应该会抛出错误,但它不会继续执行如果它们不这样做,而是将带有 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
4

2 回答 2

5

try-catch-statement 用于捕获和处理异常。如果索引超出范围、访问设置为 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在文本框的属性中插入所需的消息。将 的BlinkStyle属性设置ErrorProviderErrorBlinkStyle.NeverBlink。您可以在代码或表单设计器中进行这些设置。

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


更新

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

于 2012-11-22T20:19:54.960 回答
2

由于性能下降,您应该始终尽可能避免 try catch,请参见下面的示例:

        //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;

        }
于 2012-11-22T19:43:11.857 回答