如何控制.Net Forms.TreeView中的复选框?

时间:2023-01-17 11:09:46

I have a .Net desktop application with a TreeView as one of the UI elements.

我有一个.Net桌面应用程序,其中TreeView作为UI元素之一。

I want to be able to multi-select that TreeView, only that isn't supported at all.

我希望能够多选那个TreeView,只是根本不支持。

So I'm adding check-boxes to the tree, My problem is that only some items are selectable, and those that aren't can't consistently cascade selections.

所以我在树上添加了复选框,我的问题是只有一些项是可选择的,而那些不能一致的级联选择。

Is there any way to disable or hide some check-boxes while displaying others?

有没有办法在显示其他人时禁用或隐藏一些复选框?

3 个解决方案

#1


4  

The default behavior of TreeView is that when the Checkboxes property is set to true, that checkboxes will be shown for all TreeNodes.

TreeView的默认行为是,当Checkboxes属性设置为true时,将显示所有TreeNode的复选框。

The behavior that you are looking for - to disable or hide some checkboxes - is a custom modification of the normal behavior. You can create a custom implementation of TreeView or TreeNode that overrides the default behavior. One other thing to try would be to use the TreeView.StateImageList property. Some sites to check out for more info:

您正在寻找的行为 - 禁用或隐藏某些复选框 - 是对正常行为的自定义修改。您可以创建覆盖默认行为的TreeView或TreeNode的自定义实现。另一件要尝试的是使用TreeView.StateImageList属性。有些网站需要了解更多信息:

#2


1  

I had a very similar problem in an editor I wrote recently. In the end, I used the TreeNode's BackColor property to determine the selection state of the node. I then wrote a handler for the SelectionChanged event that checked the state of the Shift/Control keys to determine if the selected node was being added to/removed from the selection or creating a new selection. There was also a Generic::List<> of the nodes that were currently selected to eliminate any tree searches.

我最近写的一个编辑器中有一个非常类似的问题。最后,我使用TreeNode的BackColor属性来确定节点的选择状态。然后,我为SelectionChanged事件编写了一个处理程序,该事件检查了Shift / Control键的状态,以确定是将所选节点添加到选择中还是从选择中删除或创建新选择。当前还选择了一个Generic :: List <>节点来消除任何树搜索。

#3


0  

MultiSelectTreeView:

Why doesn't .NET have a multiselect treeview? There are so many uses for one and turning on checkboxes in the treeview is a pretty lousy alternative.

为什么.NET没有多选树视图?它有很多用途,在树视图中打开复选框是一个非常糟糕的选择。

#1


4  

The default behavior of TreeView is that when the Checkboxes property is set to true, that checkboxes will be shown for all TreeNodes.

TreeView的默认行为是,当Checkboxes属性设置为true时,将显示所有TreeNode的复选框。

The behavior that you are looking for - to disable or hide some checkboxes - is a custom modification of the normal behavior. You can create a custom implementation of TreeView or TreeNode that overrides the default behavior. One other thing to try would be to use the TreeView.StateImageList property. Some sites to check out for more info:

您正在寻找的行为 - 禁用或隐藏某些复选框 - 是对正常行为的自定义修改。您可以创建覆盖默认行为的TreeView或TreeNode的自定义实现。另一件要尝试的是使用TreeView.StateImageList属性。有些网站需要了解更多信息:

#2


1  

I had a very similar problem in an editor I wrote recently. In the end, I used the TreeNode's BackColor property to determine the selection state of the node. I then wrote a handler for the SelectionChanged event that checked the state of the Shift/Control keys to determine if the selected node was being added to/removed from the selection or creating a new selection. There was also a Generic::List<> of the nodes that were currently selected to eliminate any tree searches.

我最近写的一个编辑器中有一个非常类似的问题。最后,我使用TreeNode的BackColor属性来确定节点的选择状态。然后,我为SelectionChanged事件编写了一个处理程序,该事件检查了Shift / Control键的状态,以确定是将所选节点添加到选择中还是从选择中删除或创建新选择。当前还选择了一个Generic :: List <>节点来消除任何树搜索。

#3


0  

MultiSelectTreeView:

Why doesn't .NET have a multiselect treeview? There are so many uses for one and turning on checkboxes in the treeview is a pretty lousy alternative.

为什么.NET没有多选树视图?它有很多用途,在树视图中打开复选框是一个非常糟糕的选择。