Microsoft KB Archive/322937

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

Article ID: 322937

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q322937



For a Microsoft Visual Basic .NET version of this article, see 311318.

IN THIS TASK
SUMMARY
 * Extend the TreeView Control
 * Create the Client Application



SUMMARY
In earlier versions of the TreeView control, a TreeViewNode 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 Visual Studio .NET 2002 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.

back to the top

Extend the TreeView control
 Create a new Windows Control Library project:  Start Visual Studio .NET or Microsoft Visual Studio 2005. On the File menu, click New, and then click Project. In the Project Types list, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types. In the Templates list, click Windows Control Library. In the Name text box, type TreeViewEX, and then click OK. In Project Explorer, rename the default class module from UserControl1.cs to TreeViewEX.cs.</li> 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 Component Designer generated code section. public class TreeViewEX : 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.cs 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.cs class module is used for this new class. To create the class, add the following class definition to TreeViewEX.cs: public class TreeNode : System.Windows.Forms.TreeNode, IDictionaryEnumerator {   private DictionaryEntry nodeEntry; private IEnumerator enumerator;

public TreeNode {       enumerator = base.Nodes.GetEnumerator; }

public string NodeKey {       get {           return nodeEntry.Key.ToString; }       set {                      nodeEntry.Key = value; }   }    public object NodeValue {       get {           return nodeEntry.Value; }

set {           nodeEntry.Value = value; }   }

public DictionaryEntry Entry {       get {           return nodeEntry; }   }    public bool MoveNext {       bool Success; Success = enumerator.MoveNext; return Success; }

public object Current {       get {           return enumerator.Current; }   }    public object Key {       get {           return nodeEntry.Key; }   }

public object Value {       get {           return nodeEntry.Value; }   }

public void Reset {       enumerator.Reset; } }                   </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>

back to the top

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

Note In Visual Studio 2005, click Visual C# under Project Types.</li> In the Templates list, click Windows Application.</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: this.treeViewEX1.Left = 10; this.treeViewEX1.Top = 10; this.treeViewEX1.Width = this.Width - 30; this.treeViewEX1.Height = (int)(this.Height * 0.6); this.treeViewEX1.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right;

this.button1.Top = treeViewEX1.Height + 20; this.button1.Left = 10; this.button1.Width = 200; this.button1.Height = 50; this.button1.Text = &quot;Add Items to TreeViewEx&quot;;

this.button2.Top = button1.Top + button1.Height + 10; this.button2.Left = 10; this.button2.Width = 200; this.button2.Height = 50; this.button2.Text = &quot;Locate Item in TreeView using the Key Value&quot;; this.Height = 400; </li> <li> Paste the following code in the Click event of Button1: TreeViewEX.TreeNode tn; string myData;

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); Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default and names the form Form1. The two files that represent the form are called Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls from the Toolbox. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/library/ms173077.aspx

</li> <li> Paste the following code in the Click event of Button2: // Locate the third node using the NodeKey. string myData; string nodeInfo;

foreach (TreeViewEX.TreeNode tn in treeViewEX1.Nodes) {   if (tn.NodeKey == &quot;node6&quot;) {       nodeInfo = &quot;Name :&quot; + tn.Text; myData = (string)tn.NodeValue; nodeInfo += &quot;Data: &quot; + myData;

MessageBox.Show(nodeInfo, &quot;Node Specific Information&quot;, MessageBoxButtons.OK, MessageBoxIcon.Information); break; } }                   </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>

back to the top

Keywords: kbhowtomaster KB322937

-

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

© Microsoft Corporation. All rights reserved.