How to create a Key property for a TreeView Node object in Visual C++ .NET or in Visual C++ 2005 (815776)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- 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 TASKSUMMARY 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
topExtend the TreeView controlTo 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.
- In the Properties window of the user
control, change the value of the AccesibleName property to
TreeViewEX.
- 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 - 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- 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.
For more information about the common language runtime support compiler options, visit the following Microsoft Web site:These steps apply to the whole article. - On the Build menu, click Build
Solution to verify that your project compiles without any
errors.
- On the File menu, click Close
Solution to close the project.
Back to the topCreate the client applicationTo create a client application that hosts the extended TreeView
control,
follow these steps:
- 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.
- Under Project Types, click Visual
C++ Projects.
- Under Templates, click Windows
Forms Application (.NET).
- In the Name box, type
ClientApp, and then click
OK.
- Add an instance of the TreeViewEX control to the default form of the Windows Forms Application
project. To do this, follow these steps:
- Right-click the Toolbox, and then
click Add/Remove Items.
- In the Customize Toolbox dialog box,
click the .NET Framework Components tab.
- Click Browse, and then locate the
TreeViewEX.dll file that you created in the "Extend the TreeView control"
section of this article.
- Click the TreeViewEX.dll file, and
then click Open.
- In the Customize Toolbox dialog box,
click OK to add the TreeViewEX control to the Toolbox with the name
TreeViewEXControl.
- Add an instance of this control to the form.
- Add two Button controls to the
form.
- 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"Add Items to TreeViewEx";
// 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"Locate Item in TreeView using the Key Value";
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. - Add the following code in the Click event of the Button1 control:
TreeViewEX::TreeNode *tn;
String *myData;
myData = S"Extra Information pertaining to Node";
// Create TreeNode objects, assign property values,
// and then add the TreeNode objects to the Nodes collection.
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 1";
tn->NodeKey = S"node1";
tn->NodeValue = String::Concat( myData , S" 1");
treeViewEXControl1->Nodes->Add(tn);
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 2";
tn->NodeKey = S"node2";
tn->NodeValue = String::Concat( myData , S" 2");
treeViewEXControl1->Nodes->Add(tn);
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 3";
tn->NodeKey = S"node3";
tn->NodeValue = String::Concat( myData , S" 3");
treeViewEXControl1->Nodes->Add(tn);
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 4";
tn->NodeKey = S"node4";
tn->NodeValue = String::Concat( myData , S" 4");
treeViewEXControl1->Nodes->Add(tn);
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 5";
tn->NodeKey = S"node5";
tn->NodeValue = String::Concat( myData , S" 5");
treeViewEXControl1->Nodes->Add(tn);
tn = new TreeViewEX::TreeNode();
tn->Text = S"This is node 6";
tn->NodeKey = S"node6";
tn->NodeValue = String::Concat( myData , S" 6");
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. - 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"node6" ) == 0 )
{
nodeInfo = String::Concat( S"Name:", tn->Text );
myData = static_cast<String *>(tn->Value);
message = String::Concat( nodeInfo, S" Data: ", myData);
MessageBox::Show(message, S"Node Specific Information", 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. - Press CTRL+F5 to run the application.
- Click Add Items to TreeViewEX to add
sample nodes to the TreeView control, and to define values for the corresponding keys and data
members.
- 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.
Back to the
top
Modification Type: | Major | Last Reviewed: | 1/5/2006 |
---|
Keywords: | kbWindowsForms kbTreeView kbButton kbControl kbHOWTOmaster KB815776 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|