Disabling a control from receiving a event in C#

I have a dialog with loads of control in it. Each and evey control will be populated during the loading sequence and it might take a while for it to get completely filled. Mean while, I don't wanna allow the user to click on any of the controls. In other words, I wanna disable the control from receiving the events and I don't wanna disable the controls (as it might look odd).Also, I don't wanna subscribe and unsubscribe for the events regular intervals. Is there any way to stop the controls from listening to the events for a brief time ??

Sudarsan Srinivasan

Upvotes: 0

Views: 5140

Answers (4)

Fredrik Mörk
Fredrik Mörk

Reputation: 158309

The whole point of disabling controls is to communicate to the user that the control cannot be used at a particular time. This is a convention that users have learned and are used to, so I would advice to follow that. Not doing that may confuse the users.

The easiest way is to disable the container in which the controls are located in, rather than disabling each and every control. A better way (or at least the way that I prefer) is to have a method that will control the Visible and Enabled properties of controls based on which state the UI is in.

Upvotes: 4

STW
STW

Reputation: 46366

The easiest way is to move the control population out of the load event (if possible). Then in Load do something like:

private bool _LoadComplete;

void OnFormLoad(Object sender, EventArgs e)
{
    _LoadComplete = true;
    InitializeControls();
    _LoadComplete = false;
}

void InitializeControls()
{
    // Populate Controls
}

void OnSomeControlEvent()
{
    if (_LoadComplete)
    {
         // Handle the event
    }
}

Edit A Couple other Ideas:

  • Set the Application.Cursor = WaitCursor (typically will disallow clicking, but not a 100% guarantee)
  • Create a "Spinner" control to let the user know that the screen is busy. When loading bring it to the front so it sits on top and covers all other controls. Once you're done loading set it to visible = false and show your other controls

Upvotes: 2

Tim Robinson
Tim Robinson

Reputation: 54734

If you just want to disable user input, then you can set the form's Enabled property to false.

This has the effect of blocking user input to any of the form's controls, without changing the appearance of the controls; it's the technique used internally by the ShowDialog method.

Upvotes: 0

Pondidum
Pondidum

Reputation: 11627

Unfortunately the only way i know of is to have a class variable (called something like _loading) and in each control handler do something like:

If (! _loading )
{
    ...
}

And in your loading code set _loading = true; once you have finished loading.

Upvotes: 1

Related Questions