How to create a Key property for a TreeView node in Visual Basic (311318)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

This article was previously published under Q311318
For a Microsoft Visual C# version of this article, see 322937.

SUMMARY

In earlier versions of the TreeView control, a TreeView node has a Key property where information that pertains to that node can be stored. You can use this key to refer to the objects in the Nodes collection. This is useful if the order of the objects in the collection can change, or if you need some type of indexed access to the underlying data. The version of the TreeView control that is included with Microsoft Visual Studio 2005 and with Microsoft Visual Studio .NET has no equivalent Key property.

If you want to use the Key property, you have two choices:
  • Use the Tag property of the TreeNode object if you must store simple information about the object.
  • If you require the collection-based indexing functionality, you can extend the TreeNode class and implement IDictionaryEnumerator.
This step-by-step article describes the second option and provides an example of how to extend the TreeNode class to add support for a Key property.

Extend the TreeView control

  1. Create a new Windows Control Library project:
    1. Start Visual Studio 2005 or Visual Studio .NET.
    2. On the File menu, click New, and then click Project.
    3. In the Project Types list, click Visual Basic Projects, and then name the project TreeViewEX.

      Note In Visual Studio 2005, click Visual Basic in the Project Types list.
    4. In Project Explorer, rename the default class module from UserControl1.vb to TreeViewEX.vb.
    5. In the Properties window for the user control, change the name of the control from UserControl1 to TreeViewEX.
  2. Inherit from the TreeView control. By default, a user control project inherits from System.Windows.Forms.UserControl. Change the class declaration so that it inherits from System.Windows.Forms.TreeView, as follows.

    Note Do not modify the "Component Designer generated code" section.
    Public Class TreeViewEX
      Inherits System.Windows.Forms.TreeView
    					
  3. Next, create a new TreeNode class that inherits from System.Windows.Forms.TreeNode. You can do this in the same TreeViewEX.vb class module, or you can create a separate Class module. This class also implements the IDictionaryEnumerator interface and adds support for enumerating the nodes from a collection. In this example, the existing TreeViewEX.vb class module is used for this new class. To create the class, add the following class definition to TreeViewEX.vb.
    Public Class TreeNode
        Inherits System.Windows.Forms.TreeNode
        Implements IDictionaryEnumerator
    
        Private nodeEntry As DictionaryEntry
        Private enumerator As IEnumerator
    
        Public Sub New()
            enumerator = MyBase.Nodes.GetEnumerator()
        End Sub
    
        Public Property NodeKey() As String
            Get
                Return nodeEntry.Key.ToString()
            End Get
    
            Set(ByVal Value As String)
                nodeEntry.Key = Value
            End Set
        End Property
    
        Public Property NodeValue() As Object
            Get
                Return nodeEntry.Value
            End Get
    
            Set(ByVal Value As Object)
                nodeEntry.Value = Value
            End Set
        End Property
    
        Public Overridable Overloads ReadOnly Property Entry() As DictionaryEntry _
            Implements IDictionaryEnumerator.Entry
    
            Get
                Return nodeEntry
            End Get
        End Property
    
        Public Overridable Overloads Function MoveNext() As Boolean _
            Implements IDictionaryEnumerator.MoveNext
    
            Dim Success As Boolean
    
            Success = enumerator.MoveNext()
            Return Success
        End Function
    
        Public Overridable Overloads ReadOnly Property Current() As Object _
            Implements IEnumerator.Current
    
            Get
                Return enumerator.Current
            End Get
        End Property
    
        Public Overridable Overloads ReadOnly Property Key() As Object _
            Implements IDictionaryEnumerator.Key
    
            Get
                Return nodeEntry.Key
            End Get
        End Property
    
        Public Overridable Overloads ReadOnly Property Value() As Object _
            Implements IDictionaryEnumerator.Value
    
            Get
                Return nodeEntry.Value
            End Get
        End Property
    
        Public Overridable Overloads Sub Reset() _
            Implements IEnumerator.Reset
    
            enumerator.Reset()
        End Sub
    End Class
    					
  4. This completes the extended TreeView control. Now verify that your project compiles with no errors by clicking Build Solution on the Build menu.

Create the client application

  1. On the File menu, click New, and then click Project.
  2. In the Project Types list, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic in the Project Types list.
  3. In the Templates list, click Windows Application, and then click OK.
  4. Add an instance of the TreeViewEX control to the default form of the Windows Application project:
    1. On the Tools menu, click Customize Toolbox.
    2. Click the .NET Framework Components tab.
    3. Click Browse, and then locate the TreeViewEX.dll file that was just created.
    4. Click OK.
    5. The TreeViewEX control is now located on the toolbox. Add an instance of this control to the form.
  5. Add two command buttons to the form.
  6. Paste the following code in the Load event of the form.
    Me.TreeViewEX1.Left = 10
    Me.TreeViewEX1.Top = 10
    Me.TreeViewEX1.Width = Me.Width - 30
    Me.TreeViewEX1.Height = CInt(Me.Height * 0.6)
    Me.TreeViewEX1.Anchor = AnchorStyles.Top Or AnchorStyles.Left _
        Or AnchorStyles.Right
    
    Me.Button1.Top = TreeViewEX1.Height + 20
    Me.Button1.Left = 10
    Me.Button1.Width = 200
    Me.Button1.Height = 50
    Me.Button1.Text = "Add Items to TreeViewEx"
    
    Me.Button2.Top = Button1.Top + Button1.Height + 10
    Me.Button2.Left = 10
    Me.Button2.Width = 200
    Me.Button2.Height = 50
    Me.Button2.Text = "Locate Item in TreeView using the Key Value"
    Me.Height = 400
    					
  7. Paste the following code in the Click event of Button1.
    Dim tn As TreeViewEX.TreeNode
    Dim myData As String
    
    myData = "Extra Information pertaining to Node"
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 1"
    tn.NodeKey = "node1"
    tn.NodeValue = myData + " 1"
    TreeViewEX1.Nodes.Add(tn)
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 2"
    tn.NodeKey = "node2"
    tn.NodeValue = myData + " 2"
    TreeViewEX1.Nodes.Add(tn)
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 3"
    tn.NodeKey = "node3"
    tn.NodeValue = myData + " 3"
    TreeViewEX1.Nodes.Add(tn)
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 4"
    tn.NodeKey = "node4"
    tn.NodeValue = myData + " 4"
    TreeViewEX1.Nodes.Add(tn)
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 5"
    tn.NodeKey = "node5"
    tn.NodeValue = myData + "5"
    TreeViewEX1.Nodes.Add(tn)
    
    tn = New TreeViewEX.TreeNode()
    tn.Text = "This is node 6"
    tn.NodeKey = "node6"
    tn.NodeValue = myData + " 6"
    TreeViewEX1.Nodes.Add(tn)
    					
  8. Paste the following code in the Click event of Button2.
    ' Locate the third node using the NodeKey
    Dim myData As String
    Dim nodeInfo As String
    Dim tn As TreeViewEX.TreeNode
    
    For Each tn In TreeViewEX1.Nodes
        If (tn.NodeKey = "node6") Then
            nodeInfo = "Name :" + tn.Text
            myData = CStr(tn.NodeValue)
            nodeInfo += "Data: " + myData
    
            MessageBox.Show(nodeInfo, "Node Specific Information", _
                MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit For
        End If
    Next
    					
  9. Click Add Items to TreeViewEX. This adds sample nodes to the TreeView and defines values for the key and data members.
  10. Click Locate Item. After you click this button, a message box appears and shows information about node 6 based on the use of the Key property in a foreach loop.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbHOWTOmaster KB311318 kbAudDeveloper