Gaddigesh
Gaddigesh

Reputation: 1953

Disabled CheckBox reacts to mouseclick after enabling

I have couple of checkboxes on my Form.

On press of a button I am doing some operation in the backend(for about 3 seconds).

Till then I am disabling checkboxes to prevent from user interaction.

But if i click on the disabled checkbox, it reacts after it gets enabled

How do I prevent this?

Upvotes: 0

Views: 313

Answers (2)

Kishore Kumar
Kishore Kumar

Reputation: 12874

You can use BackgroundWorker to prevent UI freeze

BackgroundWorker bw = new BackgroundWorker();


bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;


private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;

    for (int i = 1; (i <= 10); i++)
    {
        if ((worker.CancellationPending == true))
        {
            e.Cancel = true;
            break;
        }
        else
        {
            // Perform a time consuming operation and report progress.
            System.Threading.Thread.Sleep(500);
            worker.ReportProgress((i * 10));
        }
    }
}


private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.tbProgress.Text = (e.ProgressPercentage.ToString() + "%");
}


private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if ((e.Cancelled == true))
    {
        this.tbProgress.Text = "Canceled!";
    }

    else if (!(e.Error == null))
    {
        this.tbProgress.Text = ("Error: " + e.Error.Message);
    }

    else
    {
        this.tbProgress.Text = "Done!";
    }
}

Upvotes: 0

Derek Harrington
Derek Harrington

Reputation: 467

I'll bet what is happening is that your UI is frozen while you are running your resource-intensive process. The mouse-click gets queued and never gets handled until after your process has finished running. By this time, checkboxes have already been re-enabled.

I would suggest using a BackgroundWorker Thread to handle your heavy 3-second operation. This will keep your UI free to handle things like mouse clicks and should fix your problem.

Upvotes: 2

Related Questions