简体   繁体   English

如何从C#中分配的图像列表中为TreeView中的父节点和子节点分配图像?

[英]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.connectedtrue hlitem.Value.connected绿色图像;如果hlitem.Value.connectedfalse 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.

相关问题 将对象分配给c#中的treeview子节点以标识父节点 - Assign object to treeview child node in c# to identify parent C#treeView,当用户单击子节点时如何获取父节点 - C# treeView, how do I get the parent nodes when user clicks the child node 列出 C# 中 TreeView 中父节点的所有子节点 - List all child node from parent node in a TreeView in C# (C#,WinForms)如何为ImageList中的图像指定辅助功能属性 - (C#, WinForms) How to assign an accessibility attribute to an image in ImageList 在Visual C#中的树视图控件中列出父节点的所有子节点 - List all child nodes of a parent node in a treeview control in Visual C# C#treeView是一个代码,用于将子节点安排为其父节点的最后一个子节点 - C# treeView is there is an code to arrange an child node to be the last child node from its parent 如何将 XML 子节点添加到父节点 c# - How to add XML child nodes to parent node c# 如何在C#中动态将子子节点添加到Treeview中的子节点? - How to add sub child nodes to a child node in Treeview dynamically in C#? 在C#树视图中展开特定节点的所有父节点 - Expand all parent nodes of a specific node in C# treeview 将节点添加到treeView中的特定父节点(C#) - Adding nodes to a specific parent node in a treeView (c#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM