简体   繁体   English

如何使用c#4.0在TreeView中获取所有选中的复选框节点名称?

[英]how to get all selected checkboxes node name in TreeView using c# 4.0?

I have a TreeView with CheckBox in my C# Windows form based application.The user select an item by clicking the checkboxes in the nodes. 我在基于C#Windows窗体的应用程序中有一个带CheckBox的TreeView。用户通过单击节点中的复选框来选择一个项目。 Now i want to get the selected checkboxes node name whenever clicking getselectedlist button pressed by the user.how i do it?. 现在我想在每次点击user.ow的时候点击getselectedlist按钮时获取所选的复选框节点名称。我这样做了吗?

Please Guide me to get out of this issue... 请指导我摆脱这个问题......

You can just use simple recursive function: 你可以使用简单的递归函数:

List<String> CheckedNames( System.Windows.Forms.TreeNodeCollection theNodes)
{
    List<String> aResult = new List<String>();

    if ( theNodes != null )
    {
        foreach ( System.Windows.Forms.TreeNode aNode in theNodes )
        {
            if ( aNode.Checked )
            {
                aResult.Add( aNode.Text );
            }

            aResult.AddRange( CheckedNames( aNode.Nodes ) );
        }
    }

    return aResult;
}

Just use it on YourTreeView.Nodes 只需在YourTreeView.Nodes上使用它

Or instead of recursively looping through every node in the TreeView every time something gets checked which could get expensive when, like me, you have hundreds or thousands of items in the list, but no more than 20 items being checked. 或者,每次检查某些内容时,不会递归地遍历TreeView中的每个节点,这可能会变得昂贵,就像我一样,您在列表中有数百或数千个项目,但检查的项目不超过20个。

I add/remove from a string list after check/uncheck since I only needed the FullPath strings, but you could probably also use a collection of TreeNode in the same way if you needed that. 我在检查/取消选中后从字符串列表中添加/删除,因为我只需要FullPath字符串,但如果需要,您也可以以相同的方式使用TreeNode的集合。

public partial class Form1 : Form
{
    List<String> CheckedNodes = new List<String>();

    public Form1()
    {
        InitializeComponent();
    }

    private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
    {
        if (e.Node.Checked)
        {
            CheckedNodes.Add(e.Node.FullPath.ToString());
        }
        else
        {
            CheckedNodes.Remove(e.Node.FullPath.ToString());
        }
    }
}
    //Uncomplicated, reliable method
    List<int> _valueList = new List<int>();
    private List<int> getCheckedNodes(TreeNodeCollection _parentNodeList)
    {
        foreach (TreeNode item in _parentNodeList)
        {
            if (item.Checked)
            {
                _valueList.Add(Convert.ToInt32(item.Value));
            }

            if (item.ChildNodes.Count > 0)
            {
                //.NET does not forget where it stayed.
                getCheckedNodes(item.ChildNodes);
            }
        } 

        return _valueList;
    }

On the button click event, you can iterate through whole tree as explained at http://msdn.microsoft.com/en-us/library/wwc698z7.aspx . 在按钮单击事件上,您可以按照http://msdn.microsoft.com/en-us/library/wwc698z7.aspx中的说明遍历整个树。 Then for each TreeNode you can check if the checkbox is checked or not and if it is checked you can add its name in to a list. 然后,对于每个TreeNode,您可以检查是否选中了复选框,如果选中它,则可以将其名称添加到列表中。

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

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