TreeView.ImageList Proprietà

Definizione

Ottiene o imposta l'oggetto ImageList che contiene gli oggetti Image che sono utilizzati dai nodi della struttura ad albero.

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; }
public System.Windows.Forms.ImageList? ImageList { get; set; }
member this.ImageList : System.Windows.Forms.ImageList with get, set
Public Property ImageList As ImageList

Valore della proprietà

Oggetto ImageList che contiene gli oggetti Image che sono utilizzati dai nodi della struttura ad albero. Il valore predefinito è null.

Esempio

Nell'esempio di codice seguente viene creato e assegnato un ImageList oggetto a un TreeView controllo e viene riempito il TreeView controllo con TreeNode oggetti . Ai nodi della struttura ad albero vengono assegnate immagini dell'oggetto ImageList che vengono visualizzate quando si trova in uno stato selezionato o non selezionato. In questo esempio è necessario disporre di un Form oggetto che contiene un TreeViewoggetto e un ArrayList oggetto che contiene Customer oggetti che ogni oggetto contiene 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

Commenti

Se il valore della ImageList proprietà è diverso da null, tutti i nodi della struttura ad albero visualizzano il primo Image oggetto archiviato in ImageList. È possibile specificare quali immagini dell'elenco vengono visualizzate per i nodi selezionati e deselezionati impostando le ImageIndex proprietà e SelectedImageIndex . I singoli TreeNode oggetti possono specificare l'immagine visualizzata impostandone ImageIndex le proprietà e SelectedImageIndex . Queste singole impostazioni sostituiranno le impostazioni nelle proprietà corrispondenti TreeView .

Nota

In .NET Framework versione 1.1 si verifica un problema che impedisce la visualizzazione delle immagini nei TreeView nodi quando l'applicazione chiama Application.EnableVisualStyles. Per risolvere questo problema, chiamare Application.DoEvents nel Main metodo immediatamente dopo la chiamata EnableVisualStylesa . Questo problema è stato risolto in .NET Framework 2.0.

Si applica a