AddHandler question.

  • Thread starter Thread starter EMW
  • Start date Start date
E

EMW

I use the following code in the POPUP event of a contextmenu:

mnuItem = New MenuItem
mnuItem.Text = "Shelter"
cmThs.MenuItems.Add(mnuItem)
AddHandler mnuItem.Click, AddressOf MenuItemClicked
mnuItem = New MenuItem
mnuItem.Text = "Building"
cmThs.MenuItems.Add(mnuItem)
AddHandler mnuItem.Click, AddressOf MenuItemClicked
mnuItem = New MenuItem
mnuItem.Text = "Bsc"
cmThs.MenuItems.Add(mnuItem)
AddHandler mnuItem.Click, AddressOf MenuItemClicked


MenuItemClicked is a function in the same form.

It works fine, but I'm having strange problems I''m wandering if this is
the
reason of it.
Simulair code is used for each textbox where the contextmenu is activated.

Now how do I remove all the handlers that were created.
I want to do this in the MenuItemClicked, because after that the handler is
not needed anymore untill the user activates the contextmenu again.
Is there some of way of removing all EventHandlers pointing to a specific
procedure?

rg,
Eric
 
Eric,

MenuItem is a part of the MenuItemCollection, in my opinion it should be
therefore very easy to do with a (see this as pseudo not tested and typed
here) to do what I thought that you where asking.

for each MnIt as MenuItem in cmThs.MenuItems
RemoveHandler MnIt.Click, AddressOf MenuItemClicked
next

I hope that this helps?

Cor
 
Hi Cor,

I also managed to solve this.
I used an array of menuitems to store each created menuitem and then in the
MenuItemClicked routine I called RemoveHandler on each arrayItem.

rg.
Eric
 
Back
Top