Akshay J
Akshay J

Reputation: 5468

Disable GUI when doing background operation

I have one form where there is a Button Copy along with other Controls. When the user presses the Copy Button, a BackgroundWorker does the copy operation.

During the copy operation I am disabling the Controls and re-enabling them upon completion. Is there anyway I can do it in an easier way. Like Disable and Enable all keyboard/Mouse input.

PS: If the user clicks while its showing the Hour-Glass icon, it should not collect all those clicks and fire them when the GUI is free. When the GUI gets free it should remove all the clicks accumulated.

Upvotes: 1

Views: 4301

Answers (3)

Rune Grimstad
Rune Grimstad

Reputation: 36340

The Form class also has the Enabled property.

By disabling the Form every control on the form will also be disabled. This is probably the easiest way to disable the entire window.

Edit:

If you really just want to just disable any input and not doing anything else, despite this being counter-intuitive and confusing for the user, then you could add a new control like a label with no content and a transparent background on top of the form.

Something like this:

// When starting the operation:
var transparentLabel = new Label();
transparentLabel.Bounds = form.Bounds;
// You may have to set the bacground and border and stuff, I am unsure
form.Children.Add(transparentLabel);
// Change cursor to a hourglass as well?


// When the operation is done
form.Children.Remove(transparentLabel);
// Change cursor back 

This should ensure that the transparent label gets focus and disables input to the other controls.

Upvotes: 0

LukeHennerley
LukeHennerley

Reputation: 6444

Set the form Enable to be false in the start, handle the finish event of the background worker and re-enable the form.

//Lock the form
this.Enabled = false;

Should do it? Excuse me if this isn't what your looking for.

EDIT:

If your looking for a fancier way of doing this you could get an animated .GIF loader from somewhere like here - http://ajaxload.info/ and then create a form with a picture box, no borders with a transparent background. Then use the GIF in there and call ShowDialog at the start and close the dialog at the end of processing, this will display a loader similar to what you see being commonly used and lock the parent form.

Upvotes: 2

Danilo Vulović
Danilo Vulović

Reputation: 3063

If you want to disable all controls, you can create new form which will be active while background process is running. You can use this form to show progress, for example.

Upvotes: 0

Related Questions