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
        myNode.Collapse
    else
        myNode.Expand
    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.

Begginnerdev commented: Looks like I was 50 seconds to slow! :D +8

You can try something like this:

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

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.

commented: If only we humans can impelement time slicing... :) +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.