简体   繁体   English

如何删除在组合框中选择的节点?

[英]How to delete node selected in ComboBox?

I want to delete child node that is selected in comboBox. 我想删除在comboBox中选择的子节点。

private void AccountsSetup_Load(object sender, EventArgs e)
{
    // Populating parent nodes with the items in Bank ComboBox that is First.
    string[] items = new string[BankList.Items.Count];

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

在此处输入图片说明

Despite of writing find node which is in Accounts comboBox, it does not find and obviously does not delete. 尽管编写了在Accounts comboBox中的find节点,但它没有找到,显然也没有删除。

// If the Account No matches to Account node, it should delete.
TreeNode[] nodes = treeView1.Nodes.Find(AccountsComboBox2.Text, true);

foreach (TreeNode oldnode in nodes)
{
    treeView1.Nodes.Remove(oldnode);
}

My add account code, maybe i am doing something wrong here: 我的添加帐户代码,也许我在这里做错了:

 treeView1.Nodes[BankList.SelectedIndex].Nodes.Add(AccountNotextBox1.Text);
        treeView1.ExpandAll();

the added account goes to AccountComboBox2. 添加的帐户转到AccountComboBox2。

so then I select AccountComboBox2.Text and matches if the node exist in treeview then delete it. 所以然后我选择AccountComboBox2.Text并匹配该节点是否在树视图中,然后将其删除。

在此处输入图片说明

Try add Cast<TreeNode> and use Where like this.But first add Tag when you create new node. 尝试添加Cast<TreeNode>并像这样使用Where 。但是在创建新节点时首先添加Tag。

First edit your function which add new node 首先编辑您的函数,添加新节点

   TreeNode newNode = new TreeNode(AccountNotextBox1.Text);
            //this is tag
            newNode.Tag = AccountsComboBox2.Text;
            treeView1.Nodes[BankList.SelectedIndex].Nodes.Add(newNode);

And next find node by Tag 然后通过Tag找到节点

TreeNode[] treeNodes = treeView1.Nodes
                                .Cast<TreeNode>()
                                .Where(r => r.Tag == AccountsComboBox2.Text)
                                .ToArray();

foreach (TreeNode oldnode in treeNodes)
 {
    if (oldnode.Parent == null)
    {
        treeView1.Nodes.Remove(oldnode);
    }
    else
    {
        oldnode.Parent.Nodes.Remove(oldnode);
    }
}

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

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