I need to Enable/Disable my SAVE button in real time based on data in my fields.
Is the below an acceptable way on accomplishing this? It feels wrong but I don’t know how else I could accomplish this.
Each User Control(CRUD Form) has a BackgroundWorker and the following related methods;
StartBGWorker()StopBGWorker()RequiredFieldsValid()
There purpose is self-explanatory, I hope. The process goes in such a way that when a User clicks NEW or EDIT it places a call to StartBGWorker() which creates a new BackgroundWorker and calls RunDataASync() for it. The DoWork() method of the BGWorker looks like this:
void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
ucNavDiagnosis.btnSave.Enabled = Convert.ToBoolean(e.UserState);
}
void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
while (true)
{
bgWorker.ReportProgress(0, RequiredFieldsValid());
System.Threading.Thread.Sleep(500);
}
}
private bool RequiredFieldsValid()
{
// TODO: Add other required fields
return (!memAllergies.Text.Equals(string.Empty));
}
This works but feels “bush-league.” Anyway, when the User clicks SAVE or CANCEL a call to StopBGWorker() is placed which Disposes the worker.
As a further disclaimer, this app does not currently use binding. It’s a long explanation but that’s that, as they say.
How about using the validating events to update the enabled status of your save button?!!
As well, if you are doing validation, I highly recommend looking into MS enterprise library validation to handle informing the user of input errors. Also, check out this.