在树视图中添加子节点 [英] adding child nodes in treeview

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

问题描述

我是C#的新手,没有任何编程经验。但是我已经完成了C#基础知识。
现在,我想通过添加父节点和子节点来设计一个简单的树形视图。

I'm new to C# and don't have any programming experience. But I've finish a C# basics. Now I would like to design a simple tree view by adding parent node and child node.

我想为Second节点添加第二个子节点,我很困在这里,不知道下一步是什么。

I would like to add a second child for the Second node, I'm quite stuck here and don't know what's next.

有什么想法吗?

这里是代码:

    private void addParentNode_Click(object sender, EventArgs e)
    {
        string yourParentNode;
        yourParentNode = textBox1.Text.Trim();
        treeView2.Nodes.Add(yourParentNode);
    }

    private void addChildNode_Click(object sender, EventArgs e)
    {
        string yourChildNode;
        yourChildNode = textBox1.Text.Trim();
        treeView2.Nodes[0].Nodes.Add(yourChildNode);
    }

对不起,我不清楚,我不确定我是否真的需要这里的这个:

Sorry I wasn't clear, I'm not sure if I really need this one here:

  //treeView1.BeginUpdate(); 
  //treeView1.Nodes.Clear();

我要做的是添加父节点和子节点。在我的代码中,我可以添加几个父节点,但是如果要添加一个子节点,则只能添加第一个父节点。
我希望如果添加一个子节点,我想将其添加到第二个父节点或第三个父节点。

What I'm trying to do, is to add Parent Nodes and child node. In my code, I can add several Parent Nodes, but if I want to add a child node, it only add in the first parent node. I want that if I add a child node, I want to add it to the second parent or third parent.

在我的代码中,我仅使用一个treeview这里的名称为treeview2
这是 截屏

In my code I only use one treeview here which names as treeview2 Here is the screenshot

这是我的最终代码:
在输入其他代码之前,如果我什么都没选择,就会出错。因此,我这样做的方式是,如果未选择任何内容,它将在默认节点或( parent1节点)中添加子节点。看来运作良好。谢谢大家;-)

this is how my final code looks like: Before I put the else, I'm getting an error if I don't select anything. So I made it that way that if there is nothing selected it will add the "child node" to the "default node" or (parent1 node). It seems to work good. Thanks guys;-)

    //This is for adding a parent node
    private void addParentNode_Click(object sender, EventArgs e)
    {
        treeView2.BeginUpdate();

        string yourParentNode;
        yourParentNode = textBox1.Text.Trim();
        treeView2.Nodes.Add(yourParentNode);
        treeView2.EndUpdate();
    }

    //This is for adding child node
    private void addChildNode_Click(object sender, EventArgs e)
    {
        if (treeView2.SelectedNode != null)
        {
            string yourChildNode;
            yourChildNode = textBox1.Text.Trim();

            treeView2.SelectedNode.Nodes.Add(yourChildNode);
            treeView2.ExpandAll();
        }
        //This is for adding the child node to the default node(parent 1 node)
        else
        {
            string yourChildNode;
            yourChildNode = textBox1.Text.Trim();
            treeView2.Nodes[0].Nodes.Add(yourChildNode);
        }

其他问题:还有其他方法可以改善代码吗?因为在这里,我两次声明了字符串 yourChildNode。如果是一个,则另一个,是否有任何简化?

Additional question: Are there any other ways on how the code be better? Because here, I declare the string "yourChildNode" twice. One in the if and other one in the else, are there any simplification?

推荐答案

还不错,但是您忘了调用 treeView2.EndUpdate()在您的 addParentNode_Click()方法中。

您还可以调用 treeView2.ExpandAll() addChildNode_Click()方法的末尾以直接查看您的子节点。

It's not that bad, but you forgot to call treeView2.EndUpdate() in your addParentNode_Click() method.
You can also call treeView2.ExpandAll() at the end of your addChildNode_Click() method to see your child node directly.

private void addParentNode_Click(object sender, EventArgs e) {
  treeView2.BeginUpdate();
  //treeView2.Nodes.Clear();
  string yourParentNode;
  yourParentNode = textBox1.Text.Trim();
  treeView2.Nodes.Add(yourParentNode);
  treeView2.EndUpdate();
}

private void addChildNode_Click(object sender, EventArgs e) {
  if (treeView2.SelectedNode != null) {
    string yourChildNode;
    yourChildNode = textBox1.Text.Trim();
    treeView2.SelectedNode.Nodes.Add(yourChildNode);
    treeView2.ExpandAll();
  }
}

我不知道这是一个错误还是没有,但是有2个TreeViews。我将其更改为仅1个TreeView ...

I don't know if it was a mistake or not but there was 2 TreeViews. I changed it to only 1 TreeView...

编辑:回答其他问题:

您可以声明包含子节点名称的变量在if子句之外:

Answer to the additional question:
You can declare the variable holding the child node name outside of the if clause:

private void addChildNode_Click(object sender, EventArgs e) {
  var childNode = textBox1.Text.Trim();
  if (!string.IsNullOrEmpty(childNode)) {
    TreeNode parentNode = treeView2.SelectedNode ?? treeView2.Nodes[0];
    if (parentNode != null) {
      parentNode.Nodes.Add(childNode);
      treeView2.ExpandAll();
    }
  }
}

注意:请参见 http://www.yoda.arachsys.com/csharp/csharp2/nullable.html 有关??的信息运算符。

Note: see http://www.yoda.arachsys.com/csharp/csharp2/nullable.html for info about the ?? operator.

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

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