Home All Groups Group Topic Archive Search About

Inherited Treeview control with dynamic context menu.

Author
12 Apr 2005 1:48 PM
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[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

Author
12 Apr 2005 4:27 PM
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

Show quoteHide quote
"Chris Murphy via DotNetMonster.com" <forum@nospam.DotNetMonster.com> wrote
in message news:e959b29f24a6478890b885cfe5360638@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[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