将节点添加到特定的父节点在一个TreeView(C#)节点、TreeView

2023-09-06 18:35:35 作者:一笑奈何

我目前加入各种价值于TreeView父节点,虽然我不能找出如何添加到特定节点树下,此刻它只是增加了选定的节点

 使用(VAR读卡器= File.OpenText(Configuration.ini))
            {
                名单<字符串>主机名= ParseExternalHosts(读者).ToList();
                的foreach(字符串s的主机名)
                {
                    树节点newNode =新的TreeNode(S);
                    hostView.SelectedNode.Nodes.Add(newNode);
                }
 

解决方案

您可以使用树视图搜索的TreeView 控制特定节点.Nodes.Find()方法。

第一下面的示例将两个节点到TreeView控件为每个节点specifing一个名称(=键)。

 常量字符串nodeKey =主机节点;

树节点TN1 =新的TreeNode(我的节点);
tn1.Name = nodeKey; //这是名称(=键)的节点。

树节点TN2 =新的TreeNode(我的节点2);
tn2.Name =otherKey; //这是关键的节点2。

treeView1.Nodes.Add(主题); //添加节点1。
treeView1.Nodes.Add(TN2); //添加节点2。
 
be due for sth造句 this cloud be due to credssp CSDN

然后,搜索比如节点1(TN1)以上使用创建以下code中的树视图:

  //查找名称(=键)节点。使用上述TN1指定的密钥。
//如果key不是唯一的,你会在这里得到一个以上的节点。
树节点[]研究发现= treeView1.Nodes.Find(nodeKey,真正的);

//做一些与查找到的节点 - 例如,只是另一个节点添加到查找到的节点。
树节点newChild对象=新的TreeNode(孩子);
newChild.Name =newChild对象;

发现[0] .Nodes.Add(newChild对象);
 

希望,这会有所帮助。

I am currently adding various values to a parent node in a treeView, although I can't find out how to add to a specific node under the tree, at the moment it simply adds to the "selected node"

 using (var reader = File.OpenText("Configuration.ini"))
            {
                List<string> hostnames = ParseExternalHosts(reader).ToList();
                foreach (string s in hostnames)
                {
                    TreeNode newNode = new TreeNode(s);
                    hostView.SelectedNode.Nodes.Add(newNode);
                }

解决方案

You can search the TreeView control for a specific node by using the TreeView.Nodes.Find() method.

The example below first adds two nodes to a TreeView control specifing a name (=key) for each node.

const string nodeKey = "hostNode";

TreeNode tn1 = new TreeNode("My Node");
tn1.Name = nodeKey; // This is the name (=key) for the node.

TreeNode tn2 = new TreeNode("My Node2");
tn2.Name = "otherKey"; // This is the key for node 2.

treeView1.Nodes.Add(tn1); // Add node1.
treeView1.Nodes.Add(tn2); // Add node2.

Then, to search for say node1 (tn1) in the tree view created above use the following code:

// Find node by name (=key). Use the key specified above for tn1.
// If key is not unique you will get more than one node here.
TreeNode[] found = treeView1.Nodes.Find(nodeKey, true);

// Do something with the found node - e.g. add just another node to the found node.
TreeNode newChild = new TreeNode("A Child");
newChild.Name = "newChild";

found[0].Nodes.Add(newChild);

Hope, this helps.

 
精彩推荐
图片推荐