|
-
Jan 28th, 2011, 07:32 AM
#1
Thread Starter
Fanatic Member
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
-
Jan 28th, 2011, 08:04 AM
#2
Re: Unwire event handlers on the fly
What happens if you do:
Code:
((CheckBox)c).CheckedState = !ticked;
-
Jan 28th, 2011, 08:30 AM
#3
Re: Unwire event handlers on the fly
Code:
((CheckBox)c).CheckState = (ticked) ? CheckState.Unchecked : CheckState.Checked;
-
Jan 28th, 2011, 08:47 AM
#4
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;
}
Delete it. They just clutter threads anyway.
-
Jan 28th, 2011, 08:53 AM
#5
Thread Starter
Fanatic Member
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
Last edited by venerable bede; Jan 28th, 2011 at 08:57 AM.
-
Jan 28th, 2011, 09:28 PM
#6
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.
-
Jan 31st, 2011, 10:52 AM
#7
Thread Starter
Fanatic Member
Re: Unwire event handlers on the fly
Yes thats correct MILK.
In fact I am trying to simulate a groupbox containing checkboxes.
-
Jan 31st, 2011, 05:41 PM
#8
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.
Last edited by Lord_Rat; Jan 31st, 2011 at 05:47 PM.
Need to re-register ASP.NET?
C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i
(Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|