将Child和Grand Child添加到树视图的Parent节点 [英] Add Child and Grandchild to Parent node of a treeview

查看:58
本文介绍了将Child和Grand Child添加到树视图的Parent节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我需要动态添加Child和Grand到父节点进行树视图,如下所示:



Treeview

- >父节点

- >子节点

- > GrandChild节点

- > GrandGrandChild Node





Hi All

I need to Add Child and Grandchild to the parentnode dynamically for a treeview as shown below:

Treeview
-->Parent Node
--> Child Node
--> GrandChild Node
--> GrandGrandChild Node


string NodeName = "Node - 1";  
            TreeNode tn = new TreeNode(NodeName ); 

            NodeName = NodeName + ".1"; 
            TreeNode tnChild  = new TreeNode(NodeName ); 

            NodeName = NodeName + ".1";
            TreeNode tnChild1 = new TreeNode(NodeName ); 
             
            NodeName = NodeName + ".1";
            TreeNode tnChild2 = new TreeNode(NodeName ); 

            tnChild1.ChildNodes.Add(tnChild2); 
            tnChild.ChildNodes.Add(tnChild1);
            tn.ChildNodes.Add(tnChild); 
            TreeView1.Nodes.Add(tn);





上面的代码表示如果深度= 4,如果depth = 3它应该停止到GrandChildNode ..为此我需要c#中的动态代码



The above code means if the depth = 4 this should happen if the depth = 3 it should stop till GrandChildNode.. For this I need a dynamic code in c#

推荐答案

您好Mathi2code,



我会使用递归初始化函数。这样的事情:(你可以运行这个例子)



Hi Mathi2code,

I''d use a recursive initialization function. something like this: (you can run the example)

using System;
using System.Windows.Forms;

namespace RecursiveTreeViewNodes
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            // Create test Form
            Form form = new Form();
            // ... and add a TreeView to it
            TreeView treeview = new TreeView() { Dock = DockStyle.Fill };
            form.Controls.Add(treeview);
            
            // Create a root node
            TreeNode nodeRoot = new TreeNode("root");
            treeview.Nodes.Add(nodeRoot);

            // Add some child node trees to the root node with different nesting depths
            AddTreeNodesRecursive(nodeRoot, 4);
            AddTreeNodesRecursive(nodeRoot, 9);
            AddTreeNodesRecursive(nodeRoot, 3);

            // Show test Form
            Application.Run(form);
        }

        static void AddTreeNodesRecursive(TreeNode nodeParent, int iDepth)
        {
            if (iDepth > 0 && nodeParent != null) 
            {
                // Create one child node for the given parent node
                TreeNode nodeChild = new TreeNode(nodeParent.Text + ".1");
                nodeParent.Nodes.Add(nodeChild);

                // Call this function again, but now the child node is the new parent and the depth is decremented.
                AddTreeNodesRecursive(nodeChild, --iDepth); // recursion !
            }
        }
    }
}





这应该只给你一般理念。如果你期望非常深的嵌套深度,你应该避免递归,如果你害怕StackOverflows,它可以重新形成一个循环。



祝你的项目好运! />


亲切问候约翰内斯



This should give you just the General idea. If if you expect very deep nesting depths you should avoid recursion, it could be reforumlated to a loop if you are fearing StackOverflows.

Good luck with your project!

kind regards Johannes



只需将添加语句粘贴到函数中即可。

ie,



private void add_child_node(string NodeName)

{

TreeNode tn = new TreeNode( NodeName +。1);

tn.ChildNodes.Add(tnChild);

TreeView1.Nodes.Add(tn);

}



每当用户需要添加节点(按钮)时调用此函数
Hi Just paste " the add statements " into a function.
ie,

private void add_child_node(string NodeName)
{
TreeNode tn = new TreeNode(NodeName+ ".1" );
tn.ChildNodes.Add(tnChild);
TreeView1.Nodes.Add(tn);
}

Call this function whenever the user needs to add the node(Push Button)


private void add_child_node(TreeNode NodeName,int noofnodes)

{

string temp = NodeName.Text;

for(int i = 0; i< noofnodes; i ++)>

{

temp + =。1;

TreeNode tn = new TreeNode(temp);

NodeName.ChildNodes.Add(tn);

}

}



其中noofnodes是no。您(或用户)需要添加到父级的节点
private void add_child_node(TreeNode NodeName,int noofnodes)
{
string temp=NodeName.Text;
for(int i=0;i<noofnodes;i++)>
{
temp += ".1";
TreeNode tn = new TreeNode(temp );
NodeName.ChildNodes.Add(tn);
}
}

where noofnodes is the no. of nodes you(or user) needs to add to the parent


这篇关于将Child和Grand Child添加到树视图的Parent节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆