Hi All,
I have a problem with a treeview class in my windows form. Specifically I'm try to detect when a user has expanded or contracted a node verses if they have clicked/selected the actual node.

When the user clicks on the node, I want to expand / contract the node to Hide / Show the child nodes (if there are any,) and if there is an associated action trigger it.
So my code goes like this:

Sub SideMenu_MouseClick(sender as Object, e as System.Windows.Forms.TreeNodeMouseClickEventArgs ) Handles SideMenu.MouseClick
    Dim MyNode As TreeViewNode = e.Node
    'here is where the problem occurs
    if myNode.IsExpanded then
    end if

    Select case trim(MyNode.Keycode)
        'Here is where I react to "action" nodes i.e. nodes that trigger actions    
    End Select

End Sub

Which works until the user clicks on the +/- box next to a node that has child nodes - The control seams to automatically expand the node then it enters my event code and recollapses or reexpands the node!!

i.e. node 1 is in an unexpanded state the user clicks the expansion button which expands it, then the MouseClick event fires sees that it is already expanded and collapses it.

I've been trying to see if there is a way to capture the button expansion but so far I can only see it happening on MouseClick - Nothing on BeforeSelect, AfterSelect etc. Mouseclick also does not seam to expose the treeviewaction to me to catch if it is Collapse or Expand.

Votes + Comments
Looks like I was 50 seconds to slow! :D
4 Years
Discussion Span
Last Post by G_Waddell

You can try something like this:

Public Sub Node_Handler(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles SideMenu.NodeMouseClick
        If e.Node.IsExpanded Then
            MsgBox("Node will close")
            'Your code here.
            MsgBox("Node will open")
            'Your Code here.
        End If
    Catch ex As Exception
    End Try
End Sub

Edited by Begginnerdev


Hi Guys,

I have invoked the 80:20 rule and decided to set showplusminus to false as I just don't have the time just now.

Votes + Comments
If only we humans can impelement time slicing... :)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.