避免在 TreeNode.ExpandAll 上扩展某些 TreeNode 节点?

发布于 2024-11-25 12:17:13 字数 357 浏览 2 评论 0原文

以前没有人问过这个问题:

当用户执行“全部展开”操作时,避免展开 WinForms TreeView 中某些 TreeNode 类后代的有效方法是什么,但仍然如此让他通过单击 + 符号来展开这些节点?

当然,我可以处理 BeforeExpand,但仅当它是 ExpandAll 时,我很难将 e.Cancel 设置为 true代码>操作。我想知道如何确定这一点?我可以继承 TreeView 并覆盖 ExpandAll ——但是那个不能被覆盖......

Nobody asked that before:

What is an efficient way to avoid the expansion of certain TreeNode class descendants in a WinForms TreeView when the user does the "Expand all" thing, but still let him expand such nodes by clicking on the + symbol?

Sure I can handle BeforeExpand, but I have a hard time setting e.Cancel to true only if it is an ExpandAll operation. I wonder how I can determine this? I could subclass TreeView and override ExpandAll -- but that one cannot be overriden...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌路黄昏 2024-12-02 12:17:13

似乎标准 .NET 树视图没有除您描述之外的其他方式:在 ExpandAll 之前触发标志,处理 BeforeExpand 并在启用标志时为适当的节点启用 e.Cancel。

由于 ExpandAll 方法不是虚拟的,因此您可以遵循以下方法:

  • 从 TreeView 类继承并添加 ExpandAllEx 方法来触发此标志。不是一个好方法,因为在使用树实例的任何地方都需要转换为树类。
  • 为 TreeView 类添加一个扩展方法,其中使用 tree.Tag 属性作为此标志。对现有代码进行最少更改的更有用的方法。

Seems like standard .NET treeview doesn`t have the way other than you described: trigger flag before ExpandAll, handle BeforeExpand and enable e.Cancel for appropriate nodes when flag is enabled.

As the ExpandAll method isn`t virtual you have these ways to follow:

  • Inherit from the TreeView class and add ExpandAllEx method where trigger this flag. No a good one because you need to cast to your tree class everywhere you use the tree instance.
  • Add an extension method for the TreeView class where use tree.Tag property for this flag. More useful way with minimal changes in existing code.
吻泪 2024-12-02 12:17:13

这100%有效。我认为。叹。

  Private Sub MyTreeViewExpandNodes(ByVal Nodes As TreeNodeCollection)
    For Each Node As TreeNode In Nodes
      If Not (TypeOf Node Is SpecialTreeNode) Then
        Node.Expand()
        MyTreeViewExpandNodes(Node.Nodes)
      End If
    Next
  End Sub

  Private Sub MyTreeView_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTreeView.KeyDown
    If e.KeyCode = Keys.Multiply Then
      e.Handled = True
      e.SuppressKeyPress = True
      MyTreeViewExpandNodes(MyTreeView.Nodes)
    End If
  End Sub

This works 100%. I think. Sigh.

  Private Sub MyTreeViewExpandNodes(ByVal Nodes As TreeNodeCollection)
    For Each Node As TreeNode In Nodes
      If Not (TypeOf Node Is SpecialTreeNode) Then
        Node.Expand()
        MyTreeViewExpandNodes(Node.Nodes)
      End If
    Next
  End Sub

  Private Sub MyTreeView_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyTreeView.KeyDown
    If e.KeyCode = Keys.Multiply Then
      e.Handled = True
      e.SuppressKeyPress = True
      MyTreeViewExpandNodes(MyTreeView.Nodes)
    End If
  End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文