[英]how to assign image for Parent node and child nodes in treeview from assigned imagelist in c#?
I have a TreeView and an associated ImageList. 我有一个TreeView和一个关联的ImageList。 What are the steps to add images to the Parent and child nodes ?
向父节点和子节点添加图像的步骤是什么?
All the nodes are being added from the code. 正在从代码中添加所有节点。 Nothing is done from the Design.
设计没有做任何事情。
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;
}
i want to add items ie child nodes to Server Parent node each of them having one image before them ( green image if hlitem.Value.connected
is true
. red image if hlitem.Value.connected
is false
) 我想向服务器父节点添加项目,即子节点,每个子节点之前都有一个图像(如果
hlitem.Value.connected
为true
hlitem.Value.connected
绿色图像;如果hlitem.Value.connected
为false
hlitem.Value.connected
红色图像)
But i have no idea about treeview or imagelist. 但是我不知道树视图或图像列表。 Can anyone help me about the whole thing?
谁能帮我解决整个问题?
The Add command returns a reference to the new Node. Add命令返回对新节点的引用。 You can use it to style the Node.
您可以使用它来设置节点的样式。 Change your code to this:
将代码更改为此:
if (treeView1.SelectedNode != null)
{
TreeNode tn =treeView1.SelectedNode.Nodes.Add(hlitem.Key.ToString());
tn.ImageIndex = yourIndex;
}
else
{
TreeNode tn =treeView1.Nodes[0].Nodes.Add(hlitem.Key.ToString());
tn.ImageIndex = yourIndex;
}
Or whatever logic you need to set the index. 或者您需要设置索引的任何逻辑。
If you need the parent node's index you could write: 如果需要父节点的索引,可以编写:
tn.ImageIndex = tn.Parent.ImageIndex;
You may also want ot check out the other formats of the Add method. 您可能还需要检查Add方法的其他格式。 Some let you include the ImageIndex directly.
有些让您直接包括ImageIndex。 You can also include the SelectedIndex;
您还可以包括SelectedIndex; especially if you don't want that you should include it to prevent the Tree using its default SelectedIndex!
特别是如果您不希望包含它,则可以使用默认的SelectedIndex阻止Tree!
This will set the node to show the 2nd image, whether selected or not: 这将设置节点显示第二张图像,无论是否选中:
TreeNode tn =treeView1.Nodes[0].Nodes.Add(sitem, sitem, 1,1 );
Since you can't set a property of an object before you have created it, you can't set the Child nodes when you create the parent node. 由于无法在创建对象之前设置对象的属性,因此在创建父节点时不能设置子节点。 Instead you can use a simple function to do the changes:
相反,您可以使用简单的函数进行更改:
void copyImgIndexToChildren(TreeNode tn)
{
if (tn.Nodes.Count > 0)
foreach (TreeNode cn in tn.Nodes) cn.ImageIndex = tn.ImageIndex;
}
void copyImgIndexToAllChildren(TreeNode tn)
{
if (tn.Nodes.Count > 0)
foreach (TreeNode cn in tn.Nodes)
{
cn.ImageIndex = tn.ImageIndex;
copyImgIndexToAllChildren(cn);
}
}
The first method changes the direct ChildNodes only , the 2nd recursively changes all levels below the starting node. 第一种方法仅更改直接的ChildNodes,第二种方法递归更改起始节点下的所有级别。
BTW: Is there a reason to use hlitem.Key.ToString()
in your code instead of sitem
? 顺便说一句:有理由在代码中使用
hlitem.Key.ToString()
代替sitem
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.