Unwire event handlers on the fly
I have a checkbox on a form.
When the user checks this I want to run a routing which loops through checkboxes on a panel control and sets their checked status to the opposite so if the initial box is checked I need to uncheck all the others.
The problem is that when I check or uncheck a box its Checked event handler is fired so I end up with a stack overflow problem.
Dont ask me why I can't use a group control. I just cant in this case
This means that I have to unwire the event handler and then rewire it when I am finished.
This is what I have :
Code:
private void ChechUncheckBoxes(CheckBox cbx)
{
bool ticked = cbx.Checked;
foreach (Control c in pnlSingle.Controls)
{
if (c is CheckBox && c.Name != cbx.Name)
{
//Need to unwire here
((CheckBox)c).Checked = !ticked; //this causes a stack overflow
//Need to rewire here
}
}
}
How do I get a handle on the delegate ?
Thanks In Advance:wave:
Re: Unwire event handlers on the fly
What happens if you do:
Code:
((CheckBox)c).CheckedState = !ticked;
Re: Unwire event handlers on the fly
Code:
((CheckBox)c).CheckState = (ticked) ? CheckState.Unchecked : CheckState.Checked;
Re: Unwire event handlers on the fly
Don't know about the rewiring, but would this work?
Code:
bool _cancelEvents = false;
private void ChechUncheckBoxes(CheckBox cbx)
{
if(this._cancelEvents) return;
this._cancelEvents = true;
bool ticked = cbx.Checked;
foreach (Control c in pnlSingle.Controls)
{
if (c is CheckBox && c.Name != cbx.Name)
{
((CheckBox)c).Checked = !ticked; //this causes a stack overflow
}
}
this._cancelEvents = false;
}
Re: Unwire event handlers on the fly
Would cancel events not cancel all events in the form?
I'm worried about other threads in the background.
I'm also working from a user control emedded on a form.
Don't have access to my PC at the mo ....grrrr
Re: Unwire event handlers on the fly
Your description of what you want seems confusing to me, I can't quite get my head around it (I'm easily confused)
You describe a checkbox on a form, and a bunch of checkboxes in a panel.
You say that when the initial checkbox is clicked all the other checkboxes want setting to the opposite.
You say that this causes an event cascade which suggests that the initial checkbox is one of those checkboxes in the panel and the code you show supports this.
This in turn suggests that your bunch of checkboxes have two basic states either all but one are checked or all but one are unchecked. Is that right? Am I missing something? Have you not told us something?
Regardless BigB's solution will only stop events that you want it to stop so it in effect it achieves what you are asking for.
Re: Unwire event handlers on the fly
Yes thats correct MILK.
In fact I am trying to simulate a groupbox containing checkboxes.
Re: Unwire event handlers on the fly
Venerable:
Code:
public static bool IsWorking = false;
public static object WorkingKey = null;
//Event handler:
bool CheckWorkingLocal = false;
lock(WorkingKey){
CheckWorkingLocal = IsWorking;
}
if(!CheckWorkingLocal){
lock(WorkingKey){
IsWorking = true;
}
//Change values here
lock(WorkingKey){
IsWorking = false;
}
// You may have to do a Sleep(0) here to let the other threads finish up.
} // Otherwise another thread is handling the event and this thread should ignore it
Basically, I am allowing the events to fire, but when they fire, the bool will be switched to "another thread is working" and they won't do anything.