Reputation: 175
This seems like a very simple and a very common problem. The simplest example I can think of is this:
The form has five checkboxes with a "check all/check none" checkbox above them. When a user selects checking all checkboxes, I toggle the states of the "children" - obviously I don't want to fire the check events of all the children until I am done setting all of the checkboxes.
I can't find a form-wide suspend control event. If I'm simply missing it then great simple answer. Barring a simple solution that I am just missing, what is the best way (best practice? accepted solution?) to suspend form control events?
Upvotes: 10
Views: 8228
Reputation: 1136
See Stack Overflow question How do I ignore simple events firing when changing control states in C# Windows Forms?
For the more general case, if you have complex relations between the controls, you could check which control fired the event by checking its own Focused property... each object is only dependent on itself.
Upvotes: 1
Reputation: 16257
In my Visual Basic 6.0 application I had to handle users double-clicking everything, so on all my event handlers I have one line that checks a global variable
Private bSuspendEvents as Boolean
Private Sub Button1_Click()
On Error Goto ErrorHandler
If bSuspendEvents then Exit Sub
bSuspendEvents = True
'Do stuff
NormalExit:
bSuspendEvents = False
Exit Sub
ErrorHandler:
'Handle Error
Resume NormalExit
End Sub
Upvotes: 1
Reputation: 35689
I've come across this before and usually seen people do this:
/*SNIP*/
private bool isMassUpdate;
public void Check1_Check(object sender, EventArgs e)
{
if(!isMassUpdate)
{
do some stuff
}
}
/*SNIP*/
You can also detach and reattach the event handlers, however, I'm told this can be a source of memory leaks.
Information on memory leaks and event handlers: They're not directly linked to attaching and detaching, but we've seen in one of our applications that bad referencing of event handlers down inheritance trees can cause it.
Upvotes: 6
Reputation: 2533
What I do in these cases instead of having a boolean value that suspends events, I use a counter. When the count is > 0, then suspend events, when the count = 0, then resume events. This helps with the problem if I have multiple things that could request a suspension of events.
The other useful thing is if I need to suspend events in a block, I create a little helper class that is IDisposable that I can use in a "using" block (in C#) so I don't forget to decrement the counter once I'm out of scope.
Upvotes: 5
Reputation: 39530
You could also consider handling 'click' events for the buttons, rather than check-changed. That might be nearer to your intent.
Upvotes: 1
Reputation: 210350
From your other question, I'm going to guess you're using VB .NET. So, RemoveHandler is your best bet. Normally in VB people set up event handlers using the Handles clause. But you can also do it this way:
AddHandler chk1.CheckedChanged, AddressOf DoSomething
where DoSomething might look like this:
Private Sub DoSomething(ByVal sender As Object, ByVal e As EventArgs)
' whatever
End Sub
AddHandler wires up the event, so it'll fire. To get it not to fire, use RemoveHandler:
RemoveHandler chk1.CheckedChanged, AddressOf DoSomething
Before updating the Checked property of your child checkboxes, call RemoveHandler on each of them; then when you're done, call AddHandler to put the event handlers back. If all your checkboxes use the same handler, you can put them in a collection and loop through the collection to add or remove the handlers.
Upvotes: 3