addhandler question

G

Guest

Guest
Archived from groups: microsoft.public.dotnet.languages.vb,microsoft.public.dotnet.vb.general,microsoft.public.pocketpc,microsoft.public.pocketpc.developer (More info?)

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
 

dennis

Distinguished
Jun 12, 2003
50
0
18,580
Archived from groups: microsoft.public.pocketpc,microsoft.public.pocketpc.developer,microsoft.public.dotnet.languages.vb (More info?)

Why don't you use a different constructor for adding MenuItems to the
collection like below instead of adding all those event handlers.

Dim pclick As EventHandler = New EventHandler(AddressOf _ MenuItemClicked)
mnuItem.Add("Shelter", pclick)
mnuItem.Add("Building", pclick)
mnuItem.Add("BSC", pclick)

Sub MenuItemClicked()
.....
End Sub

"EMW" wrote:

> 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
>
>
>
>
>
 

dennis

Distinguished
Jun 12, 2003
50
0
18,580
Archived from groups: microsoft.public.pocketpc,microsoft.public.pocketpc.developer,microsoft.public.dotnet.languages.vb (More info?)

Sorry, the code should read: (assuming cmThs is your Context Menu Object)
..........
Dim pclick As EventHandler = New EventHandler(AddressOf _ MenuItemClicked)
cmThs.MenuItems.Add("Shelter", pclick)
cmThs.MenuItems.Add("Building", pclick)
cmThs.MenuItems.Add("BSC", pclick)
End Sub

Sub MenuItemClicked(ByVal sender As Object, ByVal e As EventArgs)
Dim m As MenuItem = CType(sender, MenuItem)
Select Case m.Text
Case "Shelter"
Case "Bulding"
Case "BSC"
End Select
.....
End Sub

"EMW" wrote:

> 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
>
>
>
>
>
 
G

Guest

Guest
Archived from groups: microsoft.public.pocketpc,microsoft.public.pocketpc.developer,microsoft.public.dotnet.languages.vb (More info?)

Jan. 5, 2005

I am not aware of a method that removes all events that a method
handles, but you can use RemoveHandler for one specific event. You can then
implement an array of menuitems whose events are set to be handled by the
method and then call removehandler for each of the items in the array. The
format for the event handler is: RemoveHandler MnuItem.Event, Addressof
HandlerMethod
I hope this helps you!


Joseph MCAD



"EMW" wrote:

> 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
>
>
>
>
>
 

dennis

Distinguished
Jun 12, 2003
50
0
18,580
Archived from groups: microsoft.public.pocketpc,microsoft.public.pocketpc.developer,microsoft.public.dotnet.languages.vb (More info?)

If I have the following code, how do I remove the Delegate "pclick" where
PopMenu is my Context Menu object.

Sub MySub()
.........
Dim pclick As EventHandler = New EventHandler(AddressOf _ MenuItemClicked)
PopMenu.mnuItems.Add("Shelter", pclick)
PopMenu.mnuItems.Add("Building", pclick)
PopMenu.mnuItems.Add("BSC", pclick)
End Sub

Sub MenuItemClicked()
.....
End Sub

pclick.Remove ("What do I use for Parameters here") Thanks for any reply.


"Joseph MCAD" wrote:

>
> Jan. 5, 2005
>
> I am not aware of a method that removes all events that a method
> handles, but you can use RemoveHandler for one specific event. You can then
> implement an array of menuitems whose events are set to be handled by the
> method and then call removehandler for each of the items in the array. The
> format for the event handler is: RemoveHandler MnuItem.Event, Addressof
> HandlerMethod
> I hope this helps you!
>
>
> Joseph MCAD
>
>
>
> "EMW" wrote:
>
> > 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
> >
> >
> >
> >
> >