다음을 통해 공유


TreeNode.SelectedImageIndex 속성

정의

트리 노드를 선택한 상태일 때 표시되는 이미지의 이미지 목록 인덱스 값을 가져오거나 설정합니다.

public:
 property int SelectedImageIndex { int get(); void set(int value); };
public int SelectedImageIndex { get; set; }
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))]
[System.Windows.Forms.RelatedImageList("TreeView.ImageList")]
public int SelectedImageIndex { get; set; }
member this.SelectedImageIndex : int with get, set
[<System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.TreeViewImageIndexConverter))>]
[<System.Windows.Forms.RelatedImageList("TreeView.ImageList")>]
member this.SelectedImageIndex : int with get, set
Public Property SelectedImageIndex As Integer

속성 값

ImageList에서 이미지의 위치를 나타내는 인덱스(0부터 시작) 값입니다.

특성

예외

.NET 5 이상: value -2 미만입니다.

예제

다음 코드 예제를 만들고 할당 ImageList 합니다 TreeView 컨트롤을 개체로 TreeNode 컨트롤을 TreeView 채웁니다. 트리 노드는 트리 노드가 선택되거나 선택되지 않은 상태일 때 표시되는 의 이미지가 ImageList 할당됩니다. 이 예제에서는 Form 를 포함하는 TreeViewArrayList 각각 개체가 포함된 Customer 개체가 있어야 Order 합니다. 또한 및 Order 개체를 Customer 정의해야 합니다.

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

설명

값은 SelectedImageIndex 속성에 할당된 에 ImageList 저장된 의 Image 인덱스 값입니다TreeView.ImageList.

SelectedImageKeySelectedImageIndex 는 상호 배타적입니다. 즉, 하나가 설정되면 다른 값은 잘못된 값으로 설정되고 무시됩니다. 속성을 설정 SelectedImageKey 하면 속성이 SelectedImageIndex 자동으로 -1로 설정됩니다. 또는 속성을 설정 SelectedImageIndex 하면 가 SelectedImageKey 자동으로 빈 문자열("")로 설정됩니다.

.NET 5 이상 버전: 연결된 ImageList 속성 값이 로 변경 null되면 속성은 SelectedImageIndex 기본값인 -1을 반환합니다. 그러나 할당된 SelectedImageIndex 값은 내부적으로 유지되며 다른 ImageList 개체가 속성에 ImageList 할당될 때 사용됩니다. 속성에 ImageList 할당된 새 ImageList 속성 값이 속성 ImageList.ImageCollection.Count 에 할당된 SelectedImageIndex 값보다 작거나 같은 경우(컬렉션이 0부터 시작하는 인덱 SelectedImageIndex 스인 경우) 속성 값이 속성 값보다 Count 작은 값으로 조정됩니다. 예를 들어 세 개의 이미지가 있고 SelectedImageIndex 속성이 ImageList 2로 설정된 단추 컨트롤을 생각해 보세요. 단추에 두 개의 이미지만 있는 새 ImageList 가 할당되면 값이 SelectedImageIndex 1로 변경됩니다.

참고

속성의 SelectedImageIndex 기본값은 에 할당된 컨트롤 TreeNodeTreeView 속성과 동일합니다SelectedImageIndex.

적용 대상

추가 정보