Microsoft KB Archive/815776

= How to create a Key property for a TreeView Node object in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 815776

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1

-







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



For a Microsoft Visual C# .NET version of this article, see 322937.

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::ComponentModel
 * System::Collections
 * System::Windows::Forms

IN THIS TASK

 * SUMMARY
 * Extend the TreeView control
 * Create the client application



SUMMARY
In earlier versions of the TreeView control, each TreeView Node object has a Key property where you can store information that pertains to the node. You can use this property to refer to the objects in the Nodes collection. This property is useful if the order of the objects in this collection changes or if you require some type of indexed access to the underlying data. The version of the TreeView control that is included with Microsoft Visual Studio .NET 2003 has no equivalent Key property.

If you want to use the functionality that the Key property provides, you have two choices:
 * Use the Tag property of the TreeNode object to store simple information about the object.
 * If you require collection-based indexing functionality, extend the TreeNode class, and then implement the IDictionaryEnumerator interface.

This step-by-step article describes the second option and also provides an example of how to extend the TreeNode class to add support for the Key property.

Back to the top

Extend the TreeView control
To create an extended TreeView control, and to implement support for the Key property, follow these steps:  Create a Microsoft Windows Control Library project. To do this, follow these steps:  Start Visual Studio .NET 2003 or Visual Studio 2005. On the File menu, click New, and then click Project. The New Project dialog box appears. Under Project Types, click Visual C++ Projects.

Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++. Under Templates, click Windows Control Library (.NET).

Note In Visual Studio 2005, Windows Control Library (.NET) is changed to Windows Forms Control Library. In the Name box, type TreeViewEX, and then click OK.</li> In the Properties window of the user control, change the value of the AccesibleName property to TreeViewEX .</li></ol> </li>  In the TreeViewEXControl.h file, make the TreeViewEXControl class inherit from the TreeView class. By default, a user control inherits from the System::Windows::Forms::UserControl class. Change the class declaration as follows so that your user control inherits from the System::Windows::Forms::TreeView class: public __gc class TreeViewEXControl : public System::Windows::Forms::TreeView </li>  Create a TreeNode class that inherits from the System::Windows::Forms::TreeNode class. This class should also implement the IDictionaryEnumerator interface to add support for enumerating the nodes that are part of a collection. To create this class, add the following class definition to the TreeViewEXControl.h file, after the TreeViewEXControl class definition: public __gc class TreeNode : public System::Windows::Forms::TreeNode, public IDictionaryEnumerator { private: DictionaryEntry nodeEntry; IDictionaryEnumerator *enumerator;

public: // Constructor TreeNode(void) {       enumerator = static_cast<IDictionaryEnumerator *>( __super::Nodes->GetEnumerator); };   // Property setter and getter functions for NodeKey __property void set_NodeKey(String *value) {       nodeEntry.Key = value; };   __property String* get_NodeKey {       return nodeEntry.Key->ToString; };

// Property setter and getter functions for NodeValue __property void set_NodeValue(Object *value) {       nodeEntry.Value = value; }   __property Object* get_NodeValue {       return nodeEntry.Value; }

// Implements IDictionaryEnumerator.Entry property getter function __property DictionaryEntry get_Entry {       return nodeEntry; }

// Implements IDictionaryEnumerator.Key property getter function __property Object* get_Key {       return nodeEntry.Key; }

// Implements IDictionaryEnumerator.Value property getter function __property Object* get_Value {       return nodeEntry.Value; }

// Implements IEnumerator.Current property __property Object* get_Current {       return enumerator->Current; }

// Implements IEnumerator.MoveNext method bool MoveNext {       bool Success; Success = enumerator->MoveNext; return Success; }

// Implements IEnumerator.Reset method void Reset {       enumerator->Reset; } }; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler options, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

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

These steps apply to the whole article. </li> On the Build menu, click Build Solution to verify that your project compiles without any errors.</li> On the File menu, click Close Solution to close the project.</li></ol>

Back to the top

Create the client application
To create a client application that hosts the extended TreeView control, follow these steps: <ol> In Visual Studio .NET or in Visual Studio 2005, on the File menu, click New, and then click Project. The New Project dialog box appears.</li> Under Project Types, click Visual C++ Projects.</li> Under Templates, click Windows Forms Application (.NET).</li> In the Name box, type ClientApp, and then click OK.</li> Add an instance of the TreeViewEX control to the default form of the Windows Forms Application project. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Right-click the Toolbox, and then click Add/Remove Items.</li> In the Customize Toolbox dialog box, click the .NET Framework Components tab.</li> <li>Click Browse, and then locate the TreeViewEX.dll file that you created in the &quot;Extend the TreeView control&quot; section of this article.</li> <li>Click the TreeViewEX.dll file, and then click Open.</li> <li>In the Customize Toolbox dialog box, click OK to add the TreeViewEX control to the Toolbox with the name TreeViewEXControl.</li> <li>Add an instance of this control to the form.</li></ol> </li> <li>Add two Button controls to the form.</li> <li> Add the following code in the Load event of the form: // Position and then size the TreeView control. this->treeViewEXControl1->Left = 10; this->treeViewEXControl1->Top = 10; this->treeViewEXControl1->Width = this->Width - 30; this->treeViewEXControl1->Height = (int)(this->Height * 0.6); this->treeViewEXControl1->Anchor = static_cast<AnchorStyles>(AnchorStyles::Top | AnchorStyles::Left | AnchorStyles::Right);

// Position and size Button1, and then change the caption. this->Button1->Top = treeViewEXControl1->Height + 20; this->Button1->Left = 10; this->Button1->Width = 200; this->Button1->Height = 50; this->Button1->Text = S&quot;Add Items to TreeViewEx&quot;;

// Position and size Button2, and then change the caption. this->Button2->Top = Button1->Top + Button1->Height + 10; this->Button2->Left = 10; this->Button2->Width = 200; this->Button2->Height = 50; this->Button2->Text = S&quot;Locate Item in TreeView using the Key Value&quot;;

this->Height = 400; Note This code changes the size, the location, and the style of the extended TreeView control and the buttons when the form is loaded at run time. This code also changes the Text property of the buttons. </li> <li> Add the following code in the Click event of the Button1 control: TreeViewEX::TreeNode *tn; String *myData;

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

// Create TreeNode objects, assign property values, // and then add the TreeNode objects to the Nodes collection. tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 1&quot;; tn->NodeKey = S&quot;node1&quot;; tn->NodeValue = String::Concat( myData, S&quot; 1&quot;); treeViewEXControl1->Nodes->Add(tn);

tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 2&quot;; tn->NodeKey = S&quot;node2&quot;; tn->NodeValue = String::Concat( myData, S&quot; 2&quot;); treeViewEXControl1->Nodes->Add(tn);

tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 3&quot;; tn->NodeKey = S&quot;node3&quot;; tn->NodeValue = String::Concat( myData, S&quot; 3&quot;); treeViewEXControl1->Nodes->Add(tn);

tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 4&quot;; tn->NodeKey = S&quot;node4&quot;; tn->NodeValue = String::Concat( myData, S&quot; 4&quot;); treeViewEXControl1->Nodes->Add(tn);

tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 5&quot;; tn->NodeKey = S&quot;node5&quot;; tn->NodeValue = String::Concat( myData, S&quot; 5&quot;); treeViewEXControl1->Nodes->Add(tn);

tn = new TreeViewEX::TreeNode; tn->Text = S&quot;This is node 6&quot;; tn->NodeKey = S&quot;node6&quot;; tn->NodeValue = String::Concat( myData, S&quot; 6&quot;); treeViewEXControl1->Nodes->Add(tn); Note This code creates the TreeNode objects, and sets the corresponding Text, NodeKey, and NodeValue properties. This code also adds these TreeNode objects to the Nodes collection of the TreeViewEx object. </li> <li> Add the following code in the Click event of the Button2 control: // Locate the sixth node by using the NodeKey property. String *myData; String *nodeInfo; String *message;

TreeViewEX::TreeNode *tn; // Iterate through TreeView nodes. for(int i=0; i<treeViewEXControl1->Nodes->Count; i++) {   tn = static_cast<TreeViewEX::TreeNode *>(treeViewEXControl1->Nodes->get_Item(i)); // Compare the Node Key of each Tree Node. if ( String::Compare( static_cast<String *>(tn->Key), S&quot;node6&quot; ) == 0 ) {       nodeInfo = String::Concat( S&quot;Name:&quot;, tn->Text ); myData = static_cast<String *>(tn->Value); message = String::Concat( nodeInfo, S&quot; Data: &quot;, myData);

MessageBox::Show(message, S&quot;Node Specific Information&quot;, MessageBoxButtons::OK, MessageBoxIcon::Information); break; } } Note This code iterates through the Nodes collection of the TreeViewEx object, retrieves each TreeNode node object, and then compares the corresponding Key property value with the value node6. If a match occurs, the code uses a message box to display the corresponding node information to the user. </li> <li>Press CTRL+F5 to run the application.</li> <li>Click Add Items to TreeViewEX to add sample nodes to the TreeView control, and to define values for the corresponding keys and data members.</li> <li>Click Locate Item. A message box appears. This message box displays information about the sixth node based on the use of the Key property in a for loop.</li></ol>

Back to the top

Keywords: kbhowtomaster kbwindowsforms kbtreeview kbbutton kbcontrol KB815776

-

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

© Microsoft Corporation. All rights reserved.