How do you keep TreeView nodes expanded?

tfowler

Regular
Joined
Aug 16, 2005
Messages
84
Location
Columbus, OH
I hate how the default TreeView contracts nodes that I have manually expanded. What I mean is that I expand two different parent nodes to see the child nodes. If I then click on one of the child nodes, the other parent node contracts. I don't like that. I expanded it, it should stay expanded. Is there any way to override this functionality?

I tried putting ExpandAll in the Click event, but then it flashes with every click...you can actually see the nodes contract and then expand back out.

Thanks for the help,

Todd
 
Handle the BeforeCollapse event, set Cancel = true when you don't want a node to collapse
 
Thanks for the reply Diesel, but that doesn't work for my situation. The BeforeCollapse event is raised only when you explicitly collapse a node by clicking on the "-" symbol, or double-clicking on a node. It is not raised when the TreeView control collapses all of the expanded nodes, after I click on one of the child nodes.

Any other ideas?

Thanks,

Todd
 
I haven't been able to reproduce the sequence you describe. When I open 2 parent nodes and click on a child node, the other parent node stays expanded.
 
Wow...how weird. I deleted my old TreeView control and added a new one, and it works fine. I had created the original form in .NET 2002, but am now using .NET 2003. Maybe they fixed it? Oh well. In any case, it is working fine now.

Thanks for all of your help,

Todd
 
I figured out what is causing it

I slowly added back in my event handlers for the TreeView and found the handler that was causing the problem:

Visual Basic:
    '-------------------------------------------------------------------
    'handler for TreeView AfterSelect event - called after a node has been
    'selected in the control
    '-------------------------------------------------------------------

    Private Sub tvwTestSeq_AfterSelect(ByVal sender As System.Object, _
    ByVal e As System.Windows.Forms.TreeViewEventArgs) _
    Handles tvwTestSeq.AfterSelect
        'don't show different icon on selection
        tvwTestSeq.SelectedImageIndex = tvwTestSeq.SelectedNode.ImageIndex
    End Sub 'tvwTestSeq_AfterSelect

I'm doing this because I don't want the image to change when a node is selected. I want it to keep the original image. I guess I'll have to handle this differently.

Todd
 
Back
Top