all groups > vb.net controls > july 2004 >
You're in the

vb.net controls

group:

AddHandler and dynamic context menus


AddHandler and dynamic context menus Dave Taylor
7/22/2004 9:37:11 AM
vb.net controls:
I have a TreeView that I wish to build dynamic context menus for. So I have
a function which examines the SelectedNode and builds the appropriate menus
in the AfterSelect event of the TreeView. I direct the Click event of each
MenuItem to one of two routines using the AddHandler mi.Click, AddressOf
MyMenuRoutine. This all works fine; however I never call RemoveHandler for
the MenuItems. Will the garbage collection in VB take care of this
automatically? Even if it does, is it more efficient for me to do this? The
list of MenuItems can be somewhat large (30 - 40 items).

Thanks

Dave Taylor
PERI

RE: AddHandler and dynamic context menus amoura NO[at]SPAM online.microsoft.com
7/23/2004 8:10:45 PM
I'm not sure if I understand you scenario, but there are a few questions here - are the objects that contain the event you are handling being collected? if so,
then it depends :) the handler delegate won't be collected until the object that contains the event it handles is collected, so if the object can throw events
after you've eliminated any references to it, then you should call removehandler.
If the objects that contain the event aren't being collected, then you'll be adding more and more handlers (it's addictive), so you should call removehandler
for each tyme you add a handler.

Hope that helps, Alex (MS VB QA)

--------------------
[quoted text, click to view]

Re: AddHandler and dynamic context menus Dave Taylor
7/26/2004 10:37:00 AM
Alex,

Thanks for the reply. I have a function which builds the menu - creates a
new ContextMenu, adds MenuItems and then calls AddHandler for each menu
item. This "buildmenu" function is called in the AfterSelect event of a
TreeView...like MyTreeView.ContextMenu = BuildMenuFunction()

What I'm wondering is if before I call the BuildMenuFunction, should I
iterate through the current ContextMenu and call RemoveHandler for each
MenuItem? Or will VB do this for me automatically. I've attached the
BuildMenuFunction code below.

Thanks again,

Dave Taylor
PERI


Private Function GetPointsMenu(ByVal mr As dsSNAP.ModulesRow) As ContextMenu

Dim pr As dsSNAP.PXPointsRow, mi As MenuItem

Dim cm As New ContextMenu

Dim dr As DataRow()

Dim spr As dsConfig.SnapPointsRow

Dim bInclude As Boolean

mi = cm.MenuItems.Add("Empty")

AddHandler mi.Click, AddressOf SelectPoint

If mr Is Nothing Then Return cm 'The module is not defined yet, so just
return an "Empty" menu item

'Get the points already defined for this Unit

dr = config.SnapPoints.Select("unit_id = " & GetUnitID().ToString)

For Each pr In PX.SnapLib.PXPoints

bInclude = True

'Do the type-appropriate checks first (fastest)

'Then search the list of points only if necessary

'Check if point is appropriate type for selected module

If pr.analog <> mr.analog Then bInclude = False

If ((pr.direction = -1) And (Not mr.input)) Or ((pr.direction = 1)
And mr.input) Then bInclude = False

'Check if point is already defined, if so, then do not include it

If bInclude Then

For Each spr In dr

If spr.name = pr.name Then

bInclude = False

Exit For

End If

Next

If bInclude Then

mi = cm.MenuItems.Add(pr.name)

AddHandler mi.Click, AddressOf SelectPoint

End If

End If

Next

Return cm

End Function



[quoted text, click to view]
here - are the objects that contain the event you are handling being
collected? if so,
[quoted text, click to view]
object that contains the event it handles is collected, so if the object can
throw events
[quoted text, click to view]
be adding more and more handlers (it's addictive), so you should call
removehandler
[quoted text, click to view]

AddThis Social Bookmark Button