我有一個TreeView和一個關聯的ImageList。將圖像添加到父節點和子節點的步驟是什麼?如何從c#中分配的圖像列表中爲父節點和子樹節點分配圖像?
所有節點都是從代碼中添加的。沒有什麼是從設計中完成的。
public void fill_tree()
{
host_listbox_new.Items.Clear();
foreach (KeyValuePair<string, host_config> hlitem in host_list)
{
string sitem = hlitem.Key;
if (host_list[sitem].sessionOptions == null)
host_list[sitem].sessionOptions = new SessionOptions();
host_list[sitem].sessionOptions.Protocol = Protocol.Sftp;
host_list[sitem].sessionOptions.HostName = host_list[sitem].ip;
host_list[sitem].sessionOptions.UserName = host_list[sitem].username;
host_list[sitem].sessionOptions.Password = host_list[sitem].password;
host_list[sitem].sessionOptions.PortNumber = Convert.ToInt32(host_list[sitem].port);
//host_list[sitem].sessionOptions.SshHostKeyFingerprint = host_list[sitem].rsa;
if (treeView1.SelectedNode != null)
{
treeView1.SelectedNode.Nodes.Add(hlitem.Key.ToString());
}
else
{
treeView1.Nodes[0].Nodes.Add(hlitem.Key.ToString());
}
}
}
private void Parent_Load(object sender, EventArgs e)
{
read_process_config();
read_host_config();
host_listbox.Items.Clear();
treeView1.BeginUpdate();
treeView1.Nodes.Add("Servers");
fill_tree();
treeView1.EndUpdate();
treeView1.ExpandAll();
connect_server_bttn.Enabled = false;
}
我想補充的項目,即子節點,以它們各自有他們面前一個圖像服務器父節點(綠色形象,如果hlitem.Value.connected
是true
。紅色的圖像,如果hlitem.Value.connected
是false
)
但我不知道關於treeview或imagelist。 任何人都可以幫助我瞭解整個事情嗎?
在'TreeNode tn ='前綴'..Nodes.Add..'命令然後你可以像這樣分配一個ImageIndex:'tn.ImageIndex = 0;' – TaW
沒有給你。你能寫得詳細嗎? @TaW –