C# 为什么 Windows.Forms.TreeView 没有 SelectedNodeChanged 事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1671153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:52:19  来源:igfitidea点击:

Why isn't there a SelectedNodeChanged event for Windows.Forms.TreeView?

c#.netwinformstreeview

提问by I. J. Kennedy

The System.Web.UI.WebControls.TreeView class offers this event, but the Forms version of TreeView doesn't. What's the equivalent in the Forms world? I'm using AfterSelect but it doesn't seem quite right. Maybe it is in fact what I'm looking for but the documentation is a bit hazy.

System.Web.UI.WebControls.TreeView 类提供此事件,但 TreeView 的 Forms 版本不提供。Forms 世界中的等价物是什么?我正在使用 AfterSelect 但它似乎不太正确。也许这实际上是我正在寻找的,但文档有点模糊。

采纳答案by Pavel Minaev

There's none in WinForms TreeView. To quote MSDN for TreeView.AfterSelect:

WinForms TreeView 中没有。引用 MSDN 的TreeView.AfterSelect

This event does not occur when the node is unselected. To detect this occurrence, handle the Control.MouseUp event and test the TreeNode.IsSelected property.

取消选择节点时不会发生此事件。要检测这种情况,请处理 Control.MouseUp 事件并测试 TreeNode.IsSelected 属性。

Yes, this sucks.

是的,这很糟糕。

回答by SLaks

There's nothing wrong with using AfterSelect.

使用没有任何问题AfterSelect

However, note that it won't fire if the selection is cleared (if SelectedNodebecomes null) Instead, you can handle MouseUp, as recommended in the documentation.

但是,请注意,如果选择被清除(如果SelectedNode变为null),它将不会触发,相反,您可以MouseUp按照文档中的建议处理。

回答by Jens

OK, this is an OOOLD question, but the problem really annoyed me. I made this little helper class -- it works for me.

好吧,这是一个 OOOLD 的问题,但这个问题真的让我很恼火。我做了这个小助手类——它对我有用。

Public Class TreeViewSelectedNodeChangeEventHandler
Public Event SelectedTreeNodeChanged(sender As Object, e As EventArgs)

Private m_selectedNode As TreeNode
Private WithEvents m_tvw As TreeView

Public Shared Function FromTree(tree As TreeView) As TreeViewSelectedNodeChangeEventHandler
    If Not IsNothing(tree) Then
        Return New TreeViewSelectedNodeChangeEventHandler(tree)
    End If
    Return Nothing
End Function

''' <summary>Assigns 'Value' to 'this' and returns 'Value'.</summary>
Private Function InLineAssign(Of V)(ByRef this As V, value As V) As V
    Dim ret = value
    this = value
    Return ret
End Function

May add other triggers, e.g. Control.Enter, MouseUp etc. etc.

可以添加其他触发器,例如 Control.Enter、MouseUp 等。

Private Sub keyUp(sender As Object, e As KeyEventArgs) Handles m_tvw.KeyUp
    If Not Me.m_selectedNode Is InLineAssign(Me.m_selectedNode, m_tvw.SelectedNode)  

Then

然后

    RaiseEvent SelectedTreeNodeChanged(m_tvw, EventArgs.Empty)
        End If
    End Sub
    Private Sub New(tv As TreeView)
        m_tvw = tv
    End Sub
End Class

回答by dobragab

There's none in WinForms TreeView. To quote MSDN for TreeView.AfterSelect:

WinForms TreeView 中没有。为 TreeView.AfterSelect 引用 MSDN:

This event does not occur when the node is unselected. To detect this occurrence, handle the Control.MouseUp event and test the TreeNode.IsSelected property.

取消选择节点时不会发生此事件。要检测这种情况,请处理 Control.MouseUp 事件并测试 TreeNode.IsSelected 属性。

You'd better use TreeView.NodeMouseClick event combined with AfterSelect. AfterSelect isn't called when you select the previously selected SelectedNode. So just call AfterSelect when necessary, e.Node helps you.

您最好将 TreeView.NodeMouseClick 事件与 AfterSelect 结合使用。当您选择先前选择的 SelectedNode 时,不会调用 AfterSelect。所以在必要时调用 AfterSelect,e.Node 可以帮助你。

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (e.Node == tv.SelectedNode)
            treeView1_AfterSelect(sender, null);
    }