简体   繁体   English

如何将子节点从文本框中添加/删除到组合框中的选定节点

[英]How to add/remove child nodes from textbox to the selected node in combobox

在此处输入图片说明

  • I'm adding items from combobox to treeview at run-time. 我在运行时将组合框中的项目添加到Treeview中。

The code goes as follows: 代码如下:

 private void Form1_Load(object sender, EventArgs e)
    {
        comboBox1.SelectedIndex = 0;

        string[] items = new string[comboBox1.Items.Count];

        for (int i = 0; i < comboBox1.Items.Count; i++)
        {
            items[i] = comboBox1.Items[i].ToString();
            treeView1.Nodes.Add(items[i]);
        }
    }

Now I want to add child node to the selected node in combobox. 现在,我想将子节点添加到组合框中的选定节点。

在此处输入图片说明

When I add child for root node - 2 it goes at the bottom, as displayed in the picture above. 当我为根节点2添加子节点时,它位于底部,如上图所示。

 private void AddChildNodeButton_Click(object sender, EventArgs e)
    {
        treeView1.Nodes.Add(comboBox1.Text, textBox1.Text);
    }

To add to a selected note, use the SelectedNode method, and instead of using the combox1 dropdown, you can just select the node in the tree, like below: 要添加到选定的注释中,请使用SelectedNode方法,而不是使用combox1下拉列表,您可以仅选择树中的节点,如下所示:

private void AddChildNodeButton_Click(object sender, EventArgs e)
{

    treeView1.SelectedNode.Nodes.Add(textBox1.Text)
}

If you want to use the combobox, it will be a little slower since you first have to search the treeview using that text 如果您想使用组合框,它会慢一些,因为您首先必须使用该文本搜索树形视图

       TreeNode[] tns=treeView1.Nodes.Find(comboBox1.Text, true);
        if (tns.Length > 0)
        {

            treeView1.SelectedNode = tns[0];
            treeView1.SelectedNode.Nodes.Add(textBox1.Text)
        }

Found the answer myself, this works for me. 自己找到答案,这对我有用。

treeView1.Nodes[comboBox1.SelectedIndex].Nodes.Add(textBox1.Text);

to next question, keeping the data save. 下一个问题,保存数据。

I want to add selected item in combobox to treeview and remove selected item in combobox. 我想将组合框中的选定项目添加到树视图中,并删除组合框中的选定项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM