954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Treeview + Small Probs

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!!!!!!!!!!!

ritu verma
Light Poster
33 posts since Apr 2009
Reputation Points: 16
Solved Threads: 1
 

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
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

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..

ritu verma
Light Poster
33 posts since Apr 2009
Reputation Points: 16
Solved Threads: 1
 

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.

GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

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???

ritu verma
Light Poster
33 posts since Apr 2009
Reputation Points: 16
Solved Threads: 1
 

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

ritu verma
Light Poster
33 posts since Apr 2009
Reputation Points: 16
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You