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
。
示例
下面的代码示例创建并分配控件ImageListTreeView,并使用对象填充TreeView控件TreeNode。 树节点分配的图像来自 ImageList 处于选定状态或未选中状态时显示的图像。 此示例要求你有一个包含一个FormTreeView对象,一个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.DoEventsEnableVisualStyles。 此问题在 .NET Framework 2.0 中修复。