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

25,979

Solution 1

There's none in WinForms TreeView. To quote MSDN for 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.

Yes, this sucks.

Solution 2

There's nothing wrong with using AfterSelect.

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

Solution 3

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

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.

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
Share:
25,979

Related videos on Youtube

I. J. Kennedy
Author by

I. J. Kennedy

[email protected]

Updated on July 09, 2020

Comments

  • I. J. Kennedy
    I. J. Kennedy almost 4 years

    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.

  • I. J. Kennedy
    I. J. Kennedy over 14 years
    Control.MouseUp? They must be kidding. What if the tree is being navigating with keys instead of the mouse?
  • Ed S.
    Ed S. over 14 years
    Well, ok, it sucks, but you could always just extend the control and add the event yourself.
  • SLaks
    SLaks over 14 years
    How can you deselect a node with the keyboard?
  • SLaks
    SLaks over 14 years
    That won't deselect a node. It'll unfocus the TreeView, but the selection will remain.
  • I. J. Kennedy
    I. J. Kennedy over 14 years
    > How can you deselect a node with the keyboard? Press an arrow key.
  • SLaks
    SLaks over 14 years
    That will still raise the AfterSelect event by selecting another node.
  • Pedro77
    Pedro77 almost 10 years
    This could be in C#... :)
  • toddmo
    toddmo over 9 years
    Setting the Sorted property of the TreeView to True will deselect the SelectedNode. If you need this, you can put it in the constructor and not in the Load event, as it may unselect the node you have already selected programmatically beforehand.
  • InteXX
    InteXX over 9 years
    @Pedro77 - You could also translate it if you want to use it ;-)
  • default.kramer
    default.kramer over 8 years
    Unfortunately when you handle MouseUp the tree view's SelectedNode property has not been updated yet.