Results 1 to 5 of 5

Thread: [RESOLVED] [2005] Cancel Contextmenu in Treeview

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Resolved [RESOLVED] [2005] Cancel Contextmenu in Treeview

    I only want to dispaly a context menu in the treeview if a node is selected.
    This is what I'm trying but it doesn't seem to work. The menu apprears if I right click in the treeview whether I'm on a node or not and will not go away until l click somewhere else.

    vb Code:
    1. Private Sub tvwCharts_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvwCharts.MouseDown
    2.         If e.Button = Windows.Forms.MouseButtons.Right Then
    3.             Me.ContextMenuStrip1.Tag = tvwCharts.GetNodeAt(e.Location)
    4.             If ContextMenuStrip1.Tag Is Nothing Then
    5.                 ContextMenuStrip1.Visible = False
    6.             End If
    7.         End If
    8.     End Sub

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Cancel Contextmenu in Treeview

    1. Set the treeview's contextmenustrip property to nothing (that is to remove ContextMenuStrip1 from your treeview contextmenustrip property)
    2. Use nodemouseclick event of the node
    Code:
    Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
            If e.Button = Windows.Forms.MouseButtons.Right Then
                'Optional: you can also select the node
                TreeView1.SelectedNode = e.Node
                ContextMenuStrip1.Visible = True
                ContextMenuStrip1.Location = TreeView1.PointToScreen(e.Location)
            End If
        End Sub

  3. #3
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] Cancel Contextmenu in Treeview

    Use the "NodeMouseClick" event.
    Edit** as shown above.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Cancel Contextmenu in Treeview

    Ah, so you mean that I use the right tool for the job.

    Thanks

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    394

    Re: [2005] Cancel Contextmenu in Treeview

    I just implemented your suggestions and it did exactly what I want. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width