Event Generating Multiple Handler Calls

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Brown

    #1

    Event Generating Multiple Handler Calls

    Hi,

    I dynamically connect a MDI parent's toolstrip button to a
    child event handler by calling the following member at
    the child's Activated and Deactivate events. It looks simple
    enough but I'm getting multiple calls to the child's event
    handler when the toolstrip button is pressed. The number
    of calls increases with the number of times the child is
    (de)activated. The behavior is consistent with the handler
    be added to a list to be called when the event is triggered
    (a behavior I haven't seen documented) but never removed.

    Can someone explain what's happening and how to correct it?

    The member walks through the list of toolstrip items. If an
    item's name matches the given name the event handler is set
    or removed.

    The code is:

    // Called by a child form to en(dis)able a toolbar item and (re)set its
    event handler
    public void EnableToolBarIt em (bool _bEnabled, string _Name,
    EventHandler _Handler)
    {
    for each (ToolStripItem Item in toolStripOTRdb. Items)
    if (Item.Text == _Name)
    {
    Item.Enabled = _bEnabled;
    if (_bEnabled)
    Item.Click += new System.EventHan dler(_Handler);
    else
    Item.Click -= new System.EventHan dler(_Handler);
    }
    } // EnableToolBarIt em

    _bEnabled is true for Activated, false for Deactivate. The other
    parameters are identical for both calls.

    Thanks,
    Gary


  • Cor Ligthert [MVP]

    #2
    Re: Event Generating Multiple Handler Calls

    Gary,

    Before I forget, one of the reasons to use Ajax is to get the winforms
    behaviour on a webpage.

    Cor

    "Gary Brown" <garyjbrown@cha rter.netschreef in bericht
    news:%23VzC2LP6 GHA.4132@TK2MSF TNGP05.phx.gbl. ..
    Hi,
    >
    I dynamically connect a MDI parent's toolstrip button to a
    child event handler by calling the following member at
    the child's Activated and Deactivate events. It looks simple
    enough but I'm getting multiple calls to the child's event
    handler when the toolstrip button is pressed. The number
    of calls increases with the number of times the child is
    (de)activated. The behavior is consistent with the handler
    be added to a list to be called when the event is triggered
    (a behavior I haven't seen documented) but never removed.
    >
    Can someone explain what's happening and how to correct it?
    >
    The member walks through the list of toolstrip items. If an
    item's name matches the given name the event handler is set
    or removed.
    >
    The code is:
    >
    // Called by a child form to en(dis)able a toolbar item and (re)set its
    event handler
    public void EnableToolBarIt em (bool _bEnabled, string _Name,
    EventHandler _Handler)
    {
    for each (ToolStripItem Item in toolStripOTRdb. Items)
    if (Item.Text == _Name)
    {
    Item.Enabled = _bEnabled;
    if (_bEnabled)
    Item.Click += new System.EventHan dler(_Handler);
    else
    Item.Click -= new System.EventHan dler(_Handler);
    }
    } // EnableToolBarIt em
    >
    _bEnabled is true for Activated, false for Deactivate. The other
    parameters are identical for both calls.
    >
    Thanks,
    Gary
    >
    >

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: Event Generating Multiple Handler Calls

      Sorry added to the wrong thread


      "Cor Ligthert [MVP]" <notmyfirstname @planet.nlschre ef in bericht
      news:eyZeZ5P6GH A.4276@TK2MSFTN GP04.phx.gbl...
      Gary,
      >
      Before I forget, one of the reasons to use Ajax is to get the winforms
      behaviour on a webpage.
      >
      Cor
      >
      "Gary Brown" <garyjbrown@cha rter.netschreef in bericht
      news:%23VzC2LP6 GHA.4132@TK2MSF TNGP05.phx.gbl. ..
      >Hi,
      >>
      >I dynamically connect a MDI parent's toolstrip button to a
      >child event handler by calling the following member at
      >the child's Activated and Deactivate events. It looks simple
      >enough but I'm getting multiple calls to the child's event
      >handler when the toolstrip button is pressed. The number
      >of calls increases with the number of times the child is
      >(de)activate d. The behavior is consistent with the handler
      >be added to a list to be called when the event is triggered
      >(a behavior I haven't seen documented) but never removed.
      >>
      >Can someone explain what's happening and how to correct it?
      >>
      >The member walks through the list of toolstrip items. If an
      >item's name matches the given name the event handler is set
      >or removed.
      >>
      >The code is:
      >>
      > // Called by a child form to en(dis)able a toolbar item and (re)set
      >its event handler
      > public void EnableToolBarIt em (bool _bEnabled, string _Name,
      >EventHandler _Handler)
      > {
      > for each (ToolStripItem Item in toolStripOTRdb. Items)
      > if (Item.Text == _Name)
      > {
      > Item.Enabled = _bEnabled;
      > if (_bEnabled)
      > Item.Click += new System.EventHan dler(_Handler);
      > else
      > Item.Click -= new System.EventHan dler(_Handler);
      > }
      > } // EnableToolBarIt em
      >>
      >_bEnabled is true for Activated, false for Deactivate. The other
      >parameters are identical for both calls.
      >>
      >Thanks,
      >Gary
      >>
      >>
      >
      >

      Comment

      Working...