Event Generating Multiple Handler Calls

G

Gary Brown

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 EnableToolBarItem (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.EventHandler(_Handler);
else
Item.Click -= new System.EventHandler(_Handler);
}
} // EnableToolBarItem

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

Thanks,
Gary
 
C

Cor Ligthert [MVP]

Gary,

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

Cor
 
C

Cor Ligthert [MVP]

Sorry added to the wrong thread


Cor Ligthert said:
Gary,

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

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top