TreeView.ImageList 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
public:
property System::Windows::Forms::ImageList ^ ImageList { System::Windows::Forms::ImageList ^ get(); void set(System::Windows::Forms::ImageList ^ value); };
public System.Windows.Forms.ImageList ImageList { get; set; }
member this.ImageList : System.Windows.Forms.ImageList with get, set
Public Property ImageList As ImageList
屬性值
ImageList,其中包含樹狀節點使用的 Image 物件。 預設值是 null
。
範例
下列程式碼範例會建立 並將 指派 ImageList 給 TreeView 控制項,並以 物件填滿 TreeView 控制項 TreeNode 。 樹狀節點會從 指派影像,這些影像 ImageList 會在選取或未選取狀態時顯示。 此範例需要您有 Form TreeView 包含 的 ,以及 ArrayList 包含 Customer
每個包含 Order
物件的 物件。
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 );
}
}
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;
}
}
private void FillTreeView()
{
// Load the images in an ImageList.
ImageList myImageList = new 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 = new 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.
foreach(Customer myCustomer in customerArray)
{
// Add a child tree node for each Order object.
int countIndex=0;
TreeNode[] myTreeNodeArray = new TreeNode[myCustomer.CustomerOrders.Count];
foreach(Order myOrder in myCustomer.CustomerOrders)
{
// Add the Order tree node to the array.
myTreeNodeArray[countIndex] = new TreeNode(myOrder.OrderID,
unselectedOrderImageIndex, selectedOrderImageIndex);
countIndex++;
}
// Add the Customer tree node.
TreeNode customerNode = new TreeNode(myCustomer.CustomerName,
unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
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
Private Sub FillTreeView()
' Load the images in an ImageList.
Dim myImageList As New 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.
Me.rootImageIndex = 2
Me.selectedCustomerImageIndex = 3
Me.unselectedCustomerImageIndex = 4
Me.selectedOrderImageIndex = 5
Me.unselectedOrderImageIndex = 6
' Create the root tree node.
Dim rootNode As New 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.
Dim myCustomer As Customer
For Each myCustomer In customerArray
' Add a child tree node for each Order object.
Dim countIndex As Integer = 0
Dim myTreeNodeArray(myCustomer.CustomerOrders.Count) As TreeNode
Dim myOrder As Order
For Each myOrder In myCustomer.CustomerOrders
' Add the Order tree node to the array.
myTreeNodeArray(countIndex) = New TreeNode(myOrder.OrderID, _
unselectedOrderImageIndex, selectedOrderImageIndex)
countIndex += 1
Next myOrder
' Add the Customer tree node.
Dim customerNode As New TreeNode(myCustomer.CustomerName, _
unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray)
myTreeView.Nodes(0).Nodes.Add(customerNode)
Next myCustomer
End Sub
備註
ImageList如果 屬性值不是 , null
則所有樹狀節點都會顯示 儲存在 ImageList 中的第一個 Image 。 您可以藉由設定 ImageIndex 和 SelectedImageIndex 屬性,指定清單中要針對選取和未選取的節點顯示哪些影像。 個別 TreeNode 物件可以藉由設定其 ImageIndex 和 SelectedImageIndex 屬性來指定顯示的影像。 這些個別設定會覆寫對應 TreeView 屬性中的設定。
注意
在 .NET Framework 1.1 版中,當應用程式呼叫 Application.EnableVisualStyles 時,會防止映射出現在 TreeView 節點上。 若要解決此問題,請在呼叫 之後立即在 方法中 Main
呼叫 Application.DoEvents EnableVisualStyles 。 此問題已在 .NET Framework 2.0 中修正。