TreeNode.ForeColor 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定樹狀節點的前景色彩。
public:
property System::Drawing::Color ForeColor { System::Drawing::Color get(); void set(System::Drawing::Color value); };
public System.Drawing.Color ForeColor { get; set; }
member this.ForeColor : System.Drawing.Color with get, set
Public Property ForeColor As Color
屬性值
樹狀節點的 Color 前景。
範例
下列程式碼範例會建立根樹狀節點,以指派子樹狀節點。 中 ArrayList 每個 Customer
物件的子樹狀節點都會加入根樹狀節點,以及指派給 Customer
物件之每個 Order
物件的子樹狀節點。 物件 Customer
會指派給 Tag 屬性,而代表 Customer
物件的樹狀節點則會以 Orange 文字顯示。 此範例會要求您已 Customer
定義 和 Order
物件、 TreeView 上的 Form 控制項,以及 ArrayList 包含 Customer
物件的具名 customerArray
。
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 );
}
}
public class Customer
{
public ArrayList CustomerOrders;
public string CustomerName;
public Customer(string myName)
{
CustomerName = myName;
CustomerOrders = new ArrayList();
}
}
public class Order
{
public string OrderID;
public Order(string myOrderID )
{
this.OrderID = myOrderID;
}
}
public void AddRootNodes()
{
// Add a root node to assign the customer nodes to.
TreeNode rootNode = new TreeNode();
rootNode.Text = "CustomerList";
// Add a main root treenode.
myTreeView.Nodes.Add(rootNode);
// Add a root treenode for each 'Customer' object in the ArrayList.
foreach(Customer myCustomer in customerArray)
{
// Add a child treenode for each Order object.
int i = 0;
TreeNode[] myTreeNodeArray = new TreeNode[5];
foreach(Order myOrder in myCustomer.CustomerOrders)
{
myTreeNodeArray[i] = new TreeNode(myOrder.OrderID);
i++;
}
TreeNode customerNode = new 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);
}
}
Public Class Customer
Public CustomerOrders As ArrayList
Public CustomerName As String
Public Sub New(myName As String)
CustomerName = myName
CustomerOrders = New ArrayList()
End Sub
End Class
Public Class Order
Public OrderID As String
Public Sub New(myOrderID As String)
Me.OrderID = myOrderID
End Sub
End Class
Public Sub AddRootNodes()
' Add a root node to assign the customer nodes to.
Dim rootNode As TreeNode
rootNode = New TreeNode()
rootNode.Text = "CustomerList"
' Add a main root treenode.
myTreeView.Nodes.Add(rootNode)
' Add a root treenode for each Customer object in the ArrayList.
Dim myCustomer As Customer
For Each myCustomer In customerArray
' Add a child treenode for each Order object.
Dim i As Integer = 0
Dim myTreeNodeArray(4) As TreeNode
Dim myOrder As Order
For Each myOrder In myCustomer.CustomerOrders
myTreeNodeArray(i) = New TreeNode(myOrder.OrderID)
i += 1
Next myOrder
Dim customerNode As New 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)
Next myCustomer
End Sub
備註
如果 null
為 , Color 則所使用的 是 ForeColor 指派樹狀節點之控制項的 TreeView 屬性值。