如何使用 C# 4.0 获取 TreeView 中所有选定的复选框节点名称?
我在基于 C# Windows 表单的应用程序中有一个带有复选框的 TreeView。用户通过单击节点中的复选框来选择一个项目。现在我想在每次单击用户按下的 getselectedlist 按钮时获取选定的复选框节点名称。我该怎么做?
请指导我摆脱这个问题......
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. Now i want to get the selected checkboxes node name whenever clicking getselectedlist button pressed by the user.how i do it?.
Please Guide me to get out of this issue...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以只使用简单的递归函数:
只需在
YourTreeView.Nodes
上使用它You can just use simple recursive function:
Just use it on
YourTreeView.Nodes
或者,不要在每次检查某些内容时递归地循环遍历 TreeView 中的每个节点,这可能会变得昂贵,因为像我一样,列表中有数百或数千个项目,但检查的项目不超过 20 个。
我在选中/取消选中后从字符串列表中添加/删除,因为我只需要 FullPath 字符串,但如果需要,您也可以以相同的方式使用 TreeNode 的集合。
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.
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.
在按钮单击事件中,您可以迭代整个树,如 http:// msdn.microsoft.com/en-us/library/wwc698z7.aspx。然后,对于每个 TreeNode,您可以检查复选框是否已选中,如果选中,您可以将其名称添加到列表中。
On the button click event, you can iterate through whole tree as explained at 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.