471,616 Members | 1,709 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

Inherited Treeview control with dynamic context menu.

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[s] to deal with click events on the context menu?

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

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #1
1 2494
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

"Chris Murphy via DotNetMonster.com" <fo***@nospam.DotNetMonster.com> wrote
in message news:e9******************************@DotNetMonste r.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[s] to deal with click events on the context menu?
Any help from you pros out there would be very, very much appreciated.

--
Message posted via http://www.dotnetmonster.com

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Peter Zentner | last post: by
4 posts views Thread by Aaron Queenan | last post: by
5 posts views Thread by SoKool | last post: by
1 post views Thread by Shailendra Batham | last post: by
5 posts views Thread by A-PK | last post: by
4 posts views Thread by Andrew Robinson | last post: by
1 post views Thread by Phill W. | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.