TreeView.SelectedImageIndex Propriedade
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Obtém ou define valor do índice da lista de imagens da imagem exibida quando um nó da árvore é selecionado.
public:
property int SelectedImageIndex { int get(); void set(int value); };
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))]
public int SelectedImageIndex { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.NoneExcludedImageIndexConverter))]
[System.Windows.Forms.RelatedImageList("ImageList")]
public int SelectedImageIndex { get; set; }
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))>]
member this.SelectedImageIndex : int with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.NoneExcludedImageIndexConverter))>]
[<System.Windows.Forms.RelatedImageList("ImageList")>]
member this.SelectedImageIndex : int with get, set
Public Property SelectedImageIndex As Integer
Valor da propriedade
Um valor de índice baseado em zero que representa a posição de um Image em um ImageList.
- Atributos
Exceções
value
é menor que -1.
Exemplos
O exemplo de código a seguir cria e atribui um ImageList a um TreeView controle e preenche o TreeView controle com TreeNode objetos. Os nós de árvore são atribuídos imagens das ImageList que são exibidas quando em um estado selecionado ou não selecionado. Este exemplo requer que você tenha um Form que contenha um TreeViewe um ArrayList que contenha Customer
objetos que contêm Order
objetos. Supõe-se também que os objetos e os Customer
objetos Order
sejam definidos.
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
Comentários
O SelectedImageIndex valor é o índice de um Image armazenado na ImageList propriedade atribuída ImageList .
Observação
Ao definir a SelectedImageIndex propriedade em tempo de execução, o TreeView identificador é recriado (consulte Control.RecreateHandle) para atualizar a aparência do controle. Isso faz com que todos os nós de árvore sejam recolhidos, exceto os selecionados TreeNode.
SelectedImageKey e SelectedImageIndex são mutuamente exclusivos, ou seja, se um for definido, o outro será definido como um valor inválido e ignorado. Se você definir a SelectedImageKey propriedade, a SelectedImageIndex propriedade será definida automaticamente como -1. Como alternativa, se você definir a SelectedImageIndex propriedade, ela SelectedImageKey será definida automaticamente como uma cadeia de caracteres vazia ("").