Results 1 to 8 of 8

Thread: Unwire event handlers on the fly

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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

    Parksie

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Unwire event handlers on the fly

    What happens if you do:

    Code:
    ((CheckBox)c).CheckedState = !ticked;
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Unwire event handlers on the fly

    Code:
    ((CheckBox)c).CheckState = (ticked) ? CheckState.Unchecked : CheckState.Checked;
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    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.

  5. #5

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    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.

    Parksie

  6. #6
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  7. #7

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Re: Unwire event handlers on the fly

    Yes thats correct MILK.

    In fact I am trying to simulate a groupbox containing checkboxes.

    Parksie

  8. #8
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    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
  •  



Click Here to Expand Forum to Full Width