Microsoft KB Archive/311318

= How to create a Key property for a TreeView node in Visual Basic =

Article ID: 311318

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



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
 Create a new Windows Control Library project:  Start Visual Studio 2005 or Visual Studio .NET. On the File menu, click New, and then click Project. 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. In Project Explorer, rename the default class module from UserControl1.vb to TreeViewEX.vb. In the Properties window for the user control, change the name of the control from UserControl1 to TreeViewEX.</li></ol> </li>  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 &quot;Component Designer generated code&quot; section. Public Class TreeViewEX Inherits System.Windows.Forms.TreeView </li>  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 </li> This completes the extended TreeView control. Now verify that your project compiles with no errors by clicking Build Solution on the Build menu.</li></ol>

Create the client application
<ol> On the File menu, click New, and then click Project.</li> In the Project Types list, click Visual Basic Projects.

Note In Visual Studio 2005, click Visual Basic in the Project Types list.</li> In the Templates list, click Windows Application, and then click OK.</li> Add an instance of the TreeViewEX control to the default form of the Windows Application project: <ol style="list-style-type: lower-alpha;"> On the Tools menu, click Customize Toolbox.</li> Click the .NET Framework Components tab.</li> Click Browse, and then locate the TreeViewEX.dll file that was just created.</li> Click OK.</li> The TreeViewEX control is now located on the toolbox. Add an instance of this control to the form.</li></ol> </li> Add two command buttons to the form.</li>  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 = &quot;Add Items to TreeViewEx&quot;

Me.Button2.Top = Button1.Top + Button1.Height + 10 Me.Button2.Left = 10 Me.Button2.Width = 200 Me.Button2.Height = 50 Me.Button2.Text = &quot;Locate Item in TreeView using the Key Value&quot; Me.Height = 400 </li>  Paste the following code in the Click event of Button1. Dim tn As TreeViewEX.TreeNode Dim myData As String

myData = &quot;Extra Information pertaining to Node&quot;

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 1&quot; tn.NodeKey = &quot;node1&quot; tn.NodeValue = myData + &quot; 1&quot; TreeViewEX1.Nodes.Add(tn)

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 2&quot; tn.NodeKey = &quot;node2&quot; tn.NodeValue = myData + &quot; 2&quot; TreeViewEX1.Nodes.Add(tn)

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 3&quot; tn.NodeKey = &quot;node3&quot; tn.NodeValue = myData + &quot; 3&quot; TreeViewEX1.Nodes.Add(tn)

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 4&quot; tn.NodeKey = &quot;node4&quot; tn.NodeValue = myData + &quot; 4&quot; TreeViewEX1.Nodes.Add(tn)

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 5&quot; tn.NodeKey = &quot;node5&quot; tn.NodeValue = myData + &quot;5&quot; TreeViewEX1.Nodes.Add(tn)

tn = New TreeViewEX.TreeNode tn.Text = &quot;This is node 6&quot; tn.NodeKey = &quot;node6&quot; tn.NodeValue = myData + &quot; 6&quot; TreeViewEX1.Nodes.Add(tn) </li>  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 = &quot;node6&quot;) Then nodeInfo = &quot;Name :&quot; + tn.Text myData = CStr(tn.NodeValue) nodeInfo += &quot;Data: &quot; + myData

MessageBox.Show(nodeInfo, &quot;Node Specific Information&quot;, _           MessageBoxButtons.OK, MessageBoxIcon.Information) Exit For End If Next </li> <li>Click Add Items to TreeViewEX. This adds sample nodes to the TreeView and defines values for the key and data members.</li> <li>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.</li></ol>

Keywords: kbvs2005swept kbvs2005applies kbhowtomaster KB311318

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.