Monday, March 7, 2011

Dynamic Context Menu on Treeview Nodes

Duplicate Of : http://stackoverflow.com/questions/2527/c-treeview-context-menus

I've got a context menu on a Treeview, when the user right clicks it supposed to change based on the currently right clicked node's tag object.

Currently I'm updating the context menu in after_select event, however this doesn't work when user right clicks to another node without selecting it.

How can I detect which node right clicked and change the context menu? Or am I doing it wrong?

From stackoverflow
  • You can use the MouseDown event and the HitTest method to find out which node was clicked.

    dr. evil : that worked nicely, thanks.
    Guge : My pleasure. Thanks for the accept.
  • Private Sub tvTables_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvTables.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            Dim M As New ContextMenuStrip
            Dim HTI As TreeViewHitTestInfo = tvTables.HitTest(e.X, e.Y)
            If HTI.Node.Level = 0 Then
                M = T1Menu
            ElseIf HTI.Node.Level = 1 Then
                M = T2Menu
            ElseIf HTI.Node.Level = 2 Then
                M = T3Menu
            End If
            tvTables.ContextMenuStrip = M
            tvTables.ContextMenuStrip.Show()
        End If
    End Sub
    

0 comments:

Post a Comment