2

I have over 40 controls (TextBox, RadioButton, CheckBoxes, etc.) on Windows Forms. Each control is registered for EventHandlers (TextChanged, CheckChanged, etc.).

I want to prevent these events from firing during initialization of the form.

Unsubscribing all events before initialization and subscribing later is laborious.

Which is the best way to achieve this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gaddigesh
  • 1,953
  • 8
  • 30
  • 41
  • all events make on the on click events, not on page Load method for the code behind files. From reading your question i think this one is the situation of urs, if not don't blame on me. – Viral Shah Sep 20 '12 at 11:29

4 Answers4

1

While you've given very little information, you could of course have used:

Boolean Loading = false;

{
    Loading = true;
    LoadData();
    // LoadData must set the fact it's finished...
}

And then in event handlers:

if (Loading)
    return;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • Thanks for your suggestion ; Yes this would work; But I wanted a way to prevent firing event handler itself, Because there are so many of them – Gaddigesh Sep 20 '12 at 11:58
  • Only problem with that is the only other way would be to remove the event handlers routines, and then try and remember which to put back afterwards.. – BugFinder Sep 20 '12 at 13:03
1

My usual stupidly simple solution is have a form property that's checked by each event handler.

e.g

private bool _inhibit = false;

private void Initialise()
{
   _inhibit = true;
   try
   {
      // initialise fields
   }  
   finally
   {
      _inhibit = false;
   }
}

Then just check the state of _inhibit in each handler.

Not sure there's an unmessy way of doing this.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

Set the event handlers after InitializeComponent in the form's constructor. Like:

form()
{
     InitializeComponent();

     // Set the event handlers to your controls like:
        this.button1.Click += new System.EventHandler(this.button1_Click);

     // Or
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hamed
  • 2,084
  • 6
  • 22
  • 42
0

You could enumerate all controls like:

private void DisableAllHandlers()
{
    foreach (var control in this.Controls)
    {
       // Use reflection
    }
}

And use sources from the article How to remove all event handlers from a control to disable handlers for selected control.

Community
  • 1
  • 1
tsionyx
  • 1,629
  • 1
  • 17
  • 34
  • Interesting solution; Let me try this – Gaddigesh Sep 20 '12 at 12:06
  • 1
    But you would need to remember what events were assigned before you started to reattach them – BugFinder Sep 20 '12 at 13:03
  • 1
    Well you can write a generic routine that nips through all teh controls on the form, then all the events on each control. Stores them, removes them. The something that nips through the store and puts them all back. Something that sounds easier than it looks depending on how comprehensive you want to make it. Extension methods for instance. Can't help but feel it's bordering on over clever myself. – Tony Hopkinson Sep 20 '12 at 13:09