TreeNode Constructor (String^, Int32, Int32, array<TreeNode^>^)
Initializes a new instance of the TreeNode class with the specified label text, child tree nodes, and images to display when the tree node is in a selected and unselected state.
Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
public: TreeNode( String^ text, int imageIndex, int selectedImageIndex, array<TreeNode^>^ children )
Parameters
- text
-
Type:
System::String^
The label Text of the new tree node.
- imageIndex
-
Type:
System::Int32
The index value of Image to display when the tree node is unselected.
- selectedImageIndex
-
Type:
System::Int32
The index value of Image to display when the tree node is selected.
- children
-
Type:
array<System.Windows.Forms::TreeNode^>^
An array of child TreeNode objects.
The text parameter value is assigned to the node's Text property and becomes the tree node label. The imageIndex and selectedImageIndex values are the index values of an Image stored in the ImageList assigned to the TreeView::ImageList property. The image referenced in the imageIndex parameter is displayed when the tree node is not selected. Likewise, the image referenced in the selectedImageIndex parameter is displayed when the tree node is in a selected state. The tree nodes that are contained in the children array are added to the TreeNodeCollection that is stored in the Nodes property.
The following code example creates and assigns an ImageList to a TreeView control and fills the TreeView control with TreeNode objects. The tree nodes are assigned images from the ImageList that is displayed when the tree node is in a selected or unselected state. This example requires that you have a Form that contains a TreeView, and an ArrayList that contains Customer objects that each contain Order objects. It also requires that the Customer and Order objects are defined.
ref class Customer { public: ArrayList^ CustomerOrders; String^ CustomerName; Customer( String^ myName ) { CustomerName = myName; CustomerOrders = gcnew ArrayList; } }; ref class Order { public: String^ OrderID; Order( String^ myOrderID ) { this->OrderID = myOrderID; } }; void FillTreeView() { // Load the images in an ImageList. ImageList^ myImageList = gcnew ImageList; myImageList->Images->Add( Image::FromFile( "Default.gif" ) ); myImageList->Images->Add( Image::FromFile( "SelectedDefault.gif" ) ); myImageList->Images->Add( Image::FromFile( "Root.gif" ) ); myImageList->Images->Add( Image::FromFile( "UnselectedCustomer.gif" ) ); myImageList->Images->Add( Image::FromFile( "SelectedCustomer.gif" ) ); myImageList->Images->Add( Image::FromFile( "UnselectedOrder.gif" ) ); myImageList->Images->Add( Image::FromFile( "SelectedOrder.gif" ) ); // Assign the ImageList to the TreeView. myTreeView->ImageList = myImageList; // Set the TreeView control's default image and selected image indexes. myTreeView->ImageIndex = 0; myTreeView->SelectedImageIndex = 1; /* Set the index of image from the ImageList for selected and unselected tree nodes.*/ this->rootImageIndex = 2; this->selectedCustomerImageIndex = 3; this->unselectedCustomerImageIndex = 4; this->selectedOrderImageIndex = 5; this->unselectedOrderImageIndex = 6; // Create the root tree node. TreeNode^ rootNode = gcnew TreeNode( "CustomerList" ); rootNode->ImageIndex = rootImageIndex; rootNode->SelectedImageIndex = rootImageIndex; // Add a main root tree node. myTreeView->Nodes->Add( rootNode ); // Add a root tree node for each Customer object in the ArrayList. IEnumerator^ myEnum = customerArray->GetEnumerator(); while ( myEnum->MoveNext() ) { Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current); // Add a child tree node for each Order object. int countIndex = 0; array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(myCustomer->CustomerOrders->Count); IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator(); while ( myEnum->MoveNext() ) { Order^ myOrder = safe_cast<Order^>(myEnum->Current); // Add the Order tree node to the array. myTreeNodeArray[ countIndex ] = gcnew TreeNode( myOrder->OrderID,unselectedOrderImageIndex,selectedOrderImageIndex ); countIndex++; } TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,unselectedCustomerImageIndex,selectedCustomerImageIndex,myTreeNodeArray ); myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode ); } }
Available since 1.1