Alex Alex - 1 year ago 164 Question

VB.Net TreeView secondary identifier

Is there a way to add a secondary identifier to a TreeView node? At the moment I am using "CategoryID=" and "RecipeID=" in the key value to differentiate between category nodes and recipe nodes, using

Node.Name.ToString.Split("=")(0) = "RecipeID"
to determine what context menus and functionality the node has. For example "RecipeID=" keys get Context menu A and "CategoryID=" keys get context menu B

tvwMain.Nodes.Add("CategoryID=" + row.Item("pkCategoryID").ToString, row.Item("CategoryName").ToString)


tvwMain.Nodes.Add("RecipeID=" + row.Item("pkRecipeID").ToString, row.Item("RecipeName").ToString)

pkCategoryID and pkRecipeID are both private keys in the database, ensuring that the keys will be unique.

But, there is one other modifier that I need to determine functionality, a true and false value. I tried adding it onto the end of the key, so I could check its value
If Node.Name.ToString.Split("=")(2) = "true"
but when using
tvwMain.Nodes.Find("CategoryID=" + row.Item("CategoryID").ToString, True)
I cant use a wildcard after the

Answer Source

You can use the Tag property of a TreeNode to store additional information about the node. Tag can store a type, or an object. In this example there is just a Form and a Treeview:

Public Class Form1

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown

        With Me.TreeView1
            With .Nodes.Add("1", "Root")
                .Nodes.Add("2", "Foo").Tag = True
                .Nodes.Add("3", "Bar").Tag = False
                .Nodes.Add("4", "Baz").Tag = True
                .Nodes.Add("5", "Wup").Tag = New List(Of String) From {"x", "y", "z"}
            End With
        End With

    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
        If TypeName(e.Node.Tag) = "Boolean" Then
            MessageBox.Show("It is " & Convert.ToString(e.Node.Tag))
        End If
    End Sub

End Class
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download