TreeView.ImageIndex プロパティ
ツリー ノードで表示される既定のイメージのイメージ リスト内でのインデックス値を取得または設定します。
Public Property ImageIndex As Integer
[C#]
public int ImageIndex {get; set;}
[C++]
public: __property int get_ImageIndex();public: __property void set_ImageIndex(int);
[JScript]
public function get ImageIndex() : int;public function set ImageIndex(int);
プロパティ値
ImageList 内の Image の位置を表す 0 から始まるインデックス。既定値は 0 です。
解説
ImageIndex 値は、 ImageList プロパティに割り当てられた ImageList オブジェクト内に格納されている Image のインデックスです。
メモ 実行時に ImageIndex プロパティを設定すると、 TreeView ハンドルが再作成され (Control.RecreateHandle のトピックを参照)、コントロールの外観が更新されます。これによって、選択されている TreeNode を除くすべてのツリー ノードの表示が折りたたまれます。
使用例
[Visual Basic, C#, C++] ImageList を作成して TreeView コントロールに割り当て、 TreeView コントロールに TreeNode オブジェクトを設定する例を次に示します。ツリー ノードには、選択状態または非選択状態のときに表示される、 ImageList からのイメージが割り当てられます。この例は、 TreeView が配置された Form があり、それぞれが Order
オブジェクトを格納している Customer
オブジェクトが配置された ArrayList があることを前提にしています。また、 Customer
オブジェクトと Order
オブジェクトが定義されていることも前提にしています。
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
[C#]
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);
}
}
[C++]
private:
void FillTreeView() {
// Load the images in an ImageList.
ImageList* myImageList = new ImageList();
myImageList->Images->Add(Image::FromFile(S"Default.gif"));
myImageList->Images->Add(Image::FromFile(S"SelectedDefault.gif"));
myImageList->Images->Add(Image::FromFile(S"Root.gif"));
myImageList->Images->Add(Image::FromFile(S"UnselectedCustomer.gif"));
myImageList->Images->Add(Image::FromFile(S"SelectedCustomer.gif"));
myImageList->Images->Add(Image::FromFile(S"UnselectedOrder.gif"));
myImageList->Images->Add(Image::FromFile(S"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(S"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 = __try_cast<Customer*>(myEnum->Current);
// Add a child tree node for each Order object.
int countIndex=0;
TreeNode* myTreeNodeArray[] = new TreeNode*[myCustomer->CustomerOrders->Count];
IEnumerator* myEnum = myCustomer->CustomerOrders->GetEnumerator();
while (myEnum->MoveNext()) {
Order* myOrder = __try_cast<Order*>(myEnum->Current);
// Add the Order tree node to the array.
myTreeNodeArray->Item[countIndex] = new TreeNode(myOrder->OrderID,
unselectedOrderImageIndex, selectedOrderImageIndex);
countIndex++;
}
// Add the Customer tree node.
TreeNode* customerNode = new TreeNode(myCustomer->CustomerName,
unselectedCustomerImageIndex, selectedCustomerImageIndex, myTreeNodeArray);
myTreeView->Nodes->Item[0]->Nodes->Add(customerNode);
}
}
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET