So I have a Windows form with a few fields to accept data and write to a SQL database. My button_click event is what handles all the work except for validating the user input. These are handling by separate methods. My question is that if a user has incorrect input, how do I reset the form after showing the messagebox? Maybe just use the button click event to start a separate method so I can control it better?
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.
}
// ...
}
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