1. 程式人生 > >C# TreeView顯示圖片和文字對應

C# TreeView顯示圖片和文字對應

首先在工具欄中拖出TreeViewImageList並在ImageList中新增圖片

this.treeView1.ShowLines = false;
this.treeView1.ShowPlusMinus = false;
this.treeView1.ShowRootLines = false;
this.treeView1.HideSelection = false;
this.treeView1.Font = new Font(Font.FontFamily, 16);

//如果你的專案沒有二級節點(所有節點下面沒有子節點)你可以按照我的做

//這裡是我想要的節點即全部是父節點下面沒有子節點

 this.treeView1.Nodes.Add("0","我的愛好");
 this.treeView1.Nodes.Add("1","本地音樂");
 this.treeView1.Nodes.Add("2","播放歷史");
 this.treeView1.Nodes.Add("3","試聽列表");

//如果你有子節點 你要像這樣來取值 this.treeView1.Nodes[0].Nodes[0].ImageIndex =*;當然如果你的節點很多的話你也可以用迴圈(如:for或forearch),因為我的節點少我可以直接這樣寫

 treeView1.ImageList = imageList1;
this.treeView1.Nodes[0].ImageIndex = 0; this.treeView1.Nodes[1].ImageIndex = 1; this.treeView1.Nodes[2].ImageIndex = 2; this.treeView1.Nodes[3].ImageIndex = 3;

//這裡因為在我們單擊的時候前面的圖片會改變,我們可以這樣設定你可以在迴圈的時候和 this.treeView1.Nodes[0].ImageIndex = *;一起寫

 this.treeView1.Nodes[0].SelectedImageIndex = 0;
 this.treeView
1.Nodes[1].SelectedImageIndex = 1; this.treeView1.Nodes[2].SelectedImageIndex = 2; this.treeView1.Nodes[3].SelectedImageIndex = 3;

下面是我的效果
這裡寫圖片描述