TreeNode::ForeColor Property

 

Gets or sets the foreground color of the tree node.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
property Color ForeColor {
	Color get();
	void set(Color value);
}

Property Value

Type: System.Drawing::Color

The foreground Color of the tree node.

If null, the Color used is the ForeColor property value of the TreeView control that the tree node is assigned to.

The following code example creates a root tree node to assign child tree nodes to. A child tree node for each Customer object in an ArrayList is added to the root tree node as well as a child tree node for each Order object assigned to the Customer object. The Customer object is assigned to the Tag property, and the tree nodes representing Customer objects are displayed with Orange text. This example requires that you have a Customer and Order object defined, a TreeView control on a Form, and an ArrayList named customerArray that contains Customer objects.

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 AddRootNodes()
   {

      // Add a root node to assign the customer nodes to.
      TreeNode^ rootNode = gcnew TreeNode;
      rootNode->Text = "CustomerList";

      // Add a main root treenode.
      myTreeView->Nodes->Add( rootNode );

      // Add a root treenode for each 'Customer' object in the ArrayList.
      IEnumerator^ myEnum = customerArray->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         Customer^ myCustomer = safe_cast<Customer^>(myEnum->Current);

         // Add a child treenode for each Order object.
         int i = 0;
         array<TreeNode^>^myTreeNodeArray = gcnew array<TreeNode^>(5);
         IEnumerator^ myEnum = myCustomer->CustomerOrders->GetEnumerator();
         while ( myEnum->MoveNext() )
         {
            Order^ myOrder = safe_cast<Order^>(myEnum->Current);
            myTreeNodeArray[ i ] = gcnew TreeNode( myOrder->OrderID );
            i++;
         }
         TreeNode^ customerNode = gcnew TreeNode( myCustomer->CustomerName,myTreeNodeArray );

         // Display the customer names with and Orange font.
         customerNode->ForeColor = Color::Orange;

         // Store the Customer Object* in the Tag property of the TreeNode.
         customerNode->Tag = myCustomer;
         myTreeView->Nodes[ 0 ]->Nodes->Add( customerNode );
      }
   }

.NET Framework
Available since 1.1
Return to top
Show: