动态添加子节点如何添加子节点

发布于 2024-10-12 05:54:52 字数 1159 浏览 2 评论 0原文

我的主窗体上有一个树视图 我

的从主窗体到主窗体的代码如下

 Buttonclick

StrNode = string.Empty;
StrNode = "Batch" + Append.Batchcnt.ToString() + "(" + strSelectedClassCode + ")";
frmmain.loadFromForm(StrNode, true, strSelectedClassCode);

在我的主窗体上我的代码如下

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode)
    {
        if (Append.oldbatchcontrol != strNode)
        {
            if (tvwACH.SelectedNode.Text == "FileHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);
            }
            if (tvwACH.SelectedNode.Text == "BatchHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended
            }
  }
        }

所以我的树视图应该如下

ACH
|->Some.txt
  |->Fileheader
    |->BatchHeader
       |->Batch1
          |->Entry1
          |->Entry2 and so on // These two should be added dynamically after that Batch1

I am having a treeview on my main form

I have my code from a from to main form is as follows

 Buttonclick

StrNode = string.Empty;
StrNode = "Batch" + Append.Batchcnt.ToString() + "(" + strSelectedClassCode + ")";
frmmain.loadFromForm(StrNode, true, strSelectedClassCode);

On my main form i have my code as follows

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode)
    {
        if (Append.oldbatchcontrol != strNode)
        {
            if (tvwACH.SelectedNode.Text == "FileHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);
            }
            if (tvwACH.SelectedNode.Text == "BatchHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended
            }
  }
        }

So that my treeview should be as follows

ACH
|->Some.txt
  |->Fileheader
    |->BatchHeader
       |->Batch1
          |->Entry1
          |->Entry2 and so on // These two should be added dynamically after that Batch1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安稳善良 2024-10-19 05:54:52

使用这个代替:

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode)
    {
        if (Append.oldbatchcontrol != strNode)
        {
            if (tvwACH.SelectedNode.Text == "FileHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);
            }
            if (tvwACH.SelectedNode.Text == "BatchHeader")
            {
                TreeNode node = tvwACH.SelectedNode.Nodes.Add(strNode,strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended
                node.Nodes.Add(...);
            }
  }
}

Use this instead :

public void loadFromForm(string strNode, bool bResult, string strStandardClsCode)
    {
        if (Append.oldbatchcontrol != strNode)
        {
            if (tvwACH.SelectedNode.Text == "FileHeader")
            {
                tvwACH.SelectedNode.Nodes.Add(strNode);
            }
            if (tvwACH.SelectedNode.Text == "BatchHeader")
            {
                TreeNode node = tvwACH.SelectedNode.Nodes.Add(strNode,strNode);// After this i have to add another node as a child to that added node and also if a node with particular name exists i would like to write the text with a count value appended
                node.Nodes.Add(...);
            }
  }
}
两仪 2024-10-19 05:54:52

您通常需要递归函数来构建树。例如:

private void AddNode(NodeParent, Data)
{
    Node oNode;

    //Check if this is the first node
    if (NodeParent ==null)
    {
         oNode = new Node(Data.Name);
    }

    //Step though each child item in the data
    foreach(DataItem in Data)
    {
        //Add the node
         this.AddNode(oNode, DataItem);
    }

    oNode.Nodes.Add(new Node(Data));
}

此代码是一个粗略的指南,但它应该给您一个想法。

You usually need a recursive function to build a tree. For example:

private void AddNode(NodeParent, Data)
{
    Node oNode;

    //Check if this is the first node
    if (NodeParent ==null)
    {
         oNode = new Node(Data.Name);
    }

    //Step though each child item in the data
    foreach(DataItem in Data)
    {
        //Add the node
         this.AddNode(oNode, DataItem);
    }

    oNode.Nodes.Add(new Node(Data));
}

This code is a rough guide, but it should give you an idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文