1

所以我有一个带有几个字段的 Windows 表单来接受数据并写入 SQL 数据库。我的 button_click 事件负责处理除了验证用户输入之外的所有工作。这些是通过不同的方法处理的。我的问题是,如果用户输入不正确,我如何在显示消息框后重置表单?也许只是使用按钮单击事件来启动一个单独的方法,以便我可以更好地控制它?

private void enter_button_Click(object sender, EventArgs e)
{
    restart:
    try {
        try {
                Validate(fname);
                Validate(lname);
                Validate(city);
                Validate(state);
        } catch (Exception ex) {
            MessageBox.Show(ex.Message);
            if (ex != null) {
                fname.Clear();
                lname.Clear();
                city.Clear();
                state.Clear();
                goto restart;
            }
        }
        try {
            exValidate(address);
        } catch (Exception ex1) {
            MessageBox.Show(ex1.Message);
            if (ex1 != null) {
                address.Clear();
                goto restart;
            }
        }
        //blah blah...write to database.
    }
    // ...
}
4

5 回答 5

2

您在按钮单击中有太多逻辑。就个人而言,我宁愿验证每个领域失去焦点。如果您不想这样做,请创建一个类似 bool ValidateForm() 的方法并将所有验证逻辑包装在那里。如果至少一个内部验证失败,则返回 false。还要创建一个类似 void ClearForm() 的方法,在其中包装所有逻辑以清除所有字段。请记住,模块化代码总是好的,这样您就可以重用您的逻辑,在这里您将清除和验证逻辑与特定事件(按钮单击)联系起来。

所以在 Enter 点击你想要的

if(!ValidateForm())
{
   MessageBox.Show("Invalid Form");
   ClearForm();
   return;
}
SaveForm();

我强烈建议不要使用 GOTO 指令!这是一种非常危险的做法,可能会将您的代码变成意大利面条式的噩梦。

希望这可以帮助。

于 2012-06-08T14:57:35.540 回答
0
if (MessageBox.Show(Exception.Message) == System.Windows.Forms.DialogResult.OK)
{
    //Clear all needed controls here
}
于 2012-06-08T14:57:55.570 回答
0

Lots of problems with your code. Why do you check if ex!=null after you display it in the MessageBox? And using goto labels is not standard in C#.

The nested try is not nice either. Seperate your code a bit, as in have a seperate function that takes care of just validating the data. Your button click handler should not have to do all this work. So you would have a validate() function that takes care of validation logic and a clearForm().

That would essentially untangle all the mess in your button click handler and it would simply have to do something along the lines of:

if (validate())
  //save to db
else
  clearForm()

Also you should rethink your design, function should only throw exception if it is really needed. Do you need to throw an exception because one form input was not in a valid format?

于 2012-06-08T15:00:33.540 回答
0

Just drop the gotos. And let the user fix the wrong entries. When he has done it, he will click on the button again. If you go to restart: you might be caught in an endless loop.

If you clear all the fields when an error occurs, the user will have to reenter all the fields. Just tell him which fields are wrong and why, so that he can just fix the bad entries and can click the button when he fixed the problems.

And by the way, using goto is really ugly. Use it only in very very rare situations!

于 2012-06-08T15:00:52.057 回答
0

My suggestion would be use a background worker thread to handle the logic and just have the background worker triggered by button_click. That way you can terminate the background worker thread if the user makes a mistake and restart the whole process via button_click.

Sources:

how to keep a control disabled till a thread ends

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

于 2012-06-08T15:51:58.437 回答