Inherited Treeview control with dynamic context menu.

  • Thread starter Chris Murphy via DotNetMonster.com
  • Start date
C

Chris Murphy via DotNetMonster.com

Hi all, I'm just wondering if any one can help me with this development
issue I'm having. I've created a customized treeview control to handle the
particular tasks to which I'll be using it. Within the control I'm
dynamically creating a context menu and assigning it to the ContextMenu
property of the control -- think of this like the default context menu for
standard textboxes -- the context menu has a number of menu items that
reflect certain operations: add/edit/delete etc.

I have NO problems creating the actual menu itself, and in the test
application it displays correctly with all the items I've added to it. What
I DO have a problem with is that I cannot seem to access the events
associated with the menu like you would on normal form.

Here are some code snippets to illustrate:

DECLARATION:
Friend WithEvents __InternalContextMenu As New ContextMenu
ROUTINE TO CREATE THE MENU:
Private Sub CreateContextMenu()
Try
'add items to the context menu
With Me.__InternalContextMenu
.MenuItems.Add("Add Node Type A")
.MenuItems.Add("Add Node Type B")
.MenuItems.Add("Add Node Type C")
.MenuItems.Add("Add Node Type D")
.MenuItems.Add("-")
.MenuItems.Add("Delete Node Type A")
.MenuItems.Add("Delete Node Type B")
.MenuItems.Add("Delete Node Type C")
.MenuItems.Add("Delete Node Type D")
End With

'Set the MyTreeView ContextMenu property
'to use this default one:
Me.ContextMenu = Me.__InternalContextMenu
Catch ex As Exception
Dim ErrHandler As New ErrorLog(ex, MyAppPath())
Exit Sub
End Try
End Sub

The routine is being called from the Treeview's constructor.

QUESTION:
How do I Add event handler to deal with click events on the context menu?

Any help from you pros out there would be very, very much appreciated.
 
M

Mike Andrews

I believe this should help you:
Private Sub CreateContextMenu()
Dim cm As New ContextMenu
Dim m As MenuItem
m = cm.MenuItems.Add("Test1")
AddHandler m.Click, AddressOf ContextMenuHandler

m = cm.MenuItems.Add("Test2")
AddHandler m.Click, AddressOf ContextMenuHandler
End Sub
Private Sub ContextMenuHandler(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Select Case CType(sender, MenuItem).Text
Case "Test1"
Case "Test2"
End Select
End Sub
 

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