I have a treeview with two nodes..I m showing the contextmenu when the user clicks on any portion of treeview ....

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            Dim N As New TreeNode()
            Me.TreeViewCategories.Nodes.Clear()
            With Me.TreeViewCategories.Nodes
                N = .Add("", "Main", 0)
                N.Nodes.Add("", "First Child", 0)
                N.Nodes.Add("", "Second Child", 0)
            End With
            TreeViewCategories.ExpandAll()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub TreeViewCategories_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeViewCategories.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMnuTreeview.Show(TreeViewCategories, e.X, e.Y)
        End If
    End Sub

Mine probs is that when i right click on Second Child,Contextmenu is showing....But aotomatically root is selected....Y....?Plz copy this code,U will get to know what i want to ask!!!!!!!!!!!

I have a treeview with two nodes..I m showing the contextmenu when the user clicks on any portion of treeview ....

Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            Dim N As New TreeNode()
            Me.TreeViewCategories.Nodes.Clear()
            With Me.TreeViewCategories.Nodes
                N = .Add("", "Main", 0)
                N.Nodes.Add("", "First Child", 0)
                N.Nodes.Add("", "Second Child", 0)
            End With
            TreeViewCategories.ExpandAll()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


    Private Sub TreeViewCategories_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeViewCategories.MouseDown
        If e.Button = Windows.Forms.MouseButtons.Right Then
            ContextMnuTreeview.Show(TreeViewCategories, e.X, e.Y)
        End If
    End Sub

Mine probs is that when i right click on Second Child,Contextmenu is showing....But aotomatically root is selected....Y....?Plz copy this code,U will get to know what i want to ask!!!!!!!!!!!

Add Following code

TreeView1.Nodes.Add("Root")
        TreeView1.Nodes(0).Nodes.Add("Work")
        TreeView1.Nodes(0).Nodes.Add("Play")

        TreeView1.Nodes(0).Nodes(0).ContextMenuStrip = ContextMenuStrip1

No no actually sir i didnt want dat, i m able to solve it.....Can u tell me how to attach here the screenshots..so dat it will be helpfull for others..cz i m newbie to this site..

If i understood your problem right then this would be the solution

Private Sub TreeViewCategories_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeViewCategories.NodeMouseClick
        If e.Button = Windows.Forms.MouseButtons.Right Then
            TreeViewCategories.SelectedNode = e.Node
            ContextMnuTreeview.Show(TreeViewCategories, e.X, e.Y)
        End If
    End Sub

You might raise the TreeViewCategories.NodeMouseClick event to get the current node.

Suppose i Add a new node,now want that the new node added automatically comes in EDIT MODE...How to do dat can somebody tell me???

i m able to solved it-

Private Sub ContextMnuAddColl_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMnuAddColl.Click
        Try
            Dim tnNew As New TreeNode
            Dim tnSelected As TreeNode
            tnSelected = TreeViewCategories.SelectedNode
            If Not tnSelected Is Nothing Then
                tnNew = tnSelected.Nodes.Add("New Collection")
                tnNew.EnsureVisible()
                tnNew.BeginEdit()
            End If
            TreeViewCategories.ExpandAll()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

Thx for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.