我目前加入各种价值于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。
然后,搜索比如节点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.