views:

44

answers:

1

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.

+1  A: 

How about using the validating events to update the enabled status of your save button?!!

protected virtual void MyTextBox_OnValidating(CancelEventArgs e)
{
     this.SaveButton.Enabled = (Validate(MyTextBox));
}

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.

Lucas B
It is simply checking the fields for data. Very simplistic but all we require at this point. No SProcs and no attempted saves. I added an example of the `RequiredFieldsValid()` method.
Refracted Paladin
Also, maybe I am being thick but neither of your links solutions solves my "on the fly" issue, no?
Refracted Paladin
@refracted what does "on the fly" mean? Are you trying to keep the UI responsive, or something else?
Lucas B
Basically, as they type...if what they type makes the required fields valid I want it to Enable the Save button and vice versa.
Refracted Paladin
I would not recommend validating while the user is providing input, only after the control loses focus; otherwise the user experience will be horrible.
Lucas B
Instead, when they click save, highlight for them what is not valid and where. The MS enterprise library is a solid framework for these types of interactions
Lucas B
:-) Thank you Lucas for the effort. Unfortunately that is what I already have implemented and it is not enough for the business. They don't want to even be able to click Save until the form is Valid...Think the way Silverlight tends to do it.
Refracted Paladin
@refracted Somehow I missed that you wanted to enable/disable the save button. I have updated my answer.
Lucas B