TreeNode.ImageIndex 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置当树节点处于未选定状态时所显示图像的图像列表索引值。
public:
property int ImageIndex { int get(); void set(int value); };
public int ImageIndex { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))]
[System.Windows.Forms.RelatedImageList("TreeView.ImageList")]
public int ImageIndex { get; set; }
member this.ImageIndex : int with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))>]
[<System.Windows.Forms.RelatedImageList("TreeView.ImageList")>]
member this.ImageIndex : int with get, set
Public Property ImageIndex As Integer
属性值
从零开始的索引值,表示图像在所分配的 ImageList 中的位置。
- 属性
例外
.NET 5 及更高版本: value
小于 -2。
示例
下面的代码示例创建 并将 分配给ImageListTreeView控件,并使用 对象填充TreeView控件TreeNode。 树节点分配的图像来自 ImageList 当树节点处于选定或未选择状态时显示的图像。 此示例要求具有一个 Form 包含 TreeView的 ,以及一个 ArrayList 包含 Customer
每个包含 Order
对象的 对象。 它还要求 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
注解
值ImageIndex是分配给 TreeView.ImageList 属性的 Image 中ImageList存储的 索引值。
注意
属性的ImageIndex默认值与 分配给 的TreeView控件TreeNode的 属性相同ImageIndex。
ImageKey 和 ImageIndex 是互斥的,这意味着如果设置了一个,另一个设置为无效值并被忽略。 如果设置 ImageKey 属性,则 ImageIndex 属性将自动设置为 -1。 或者,如果设置 ImageIndex 属性,则 ImageKey 会自动将 设置为空字符串 (“”) 。
.NET 5 及更高版本: 如果关联的 ImageList 属性值更改为 null
,则 ImageIndex 属性返回其默认值 -1。 但是,分配 ImageIndex 的值在内部保留,并在将另一个 ImageList 对象分配给 ImageList 属性时使用。 如果分配给属性ImageList.ImageCollection.CountImageList的新ImageList属性值小于或等于分配给ImageIndex属性的值减去 1 (以说明集合是一个从零开始的索引) ,则ImageIndex属性值将调整为比Count属性值少一个。
例如,假设一个按钮控件具有 ImageList 三个图像,其 ImageIndex 属性设置为 2。 如果将只有两个图像的新 ImageList 分配给按钮,该值 ImageIndex 将更改为 1。