Question

I'm looking for pretty simple functionality, however I cannot find it (and surprisingly I cannot find anyone else asking this question).

I've got split container with two panels - pretty simple setup, navigational TreeView on the left, ListView with items on the right.

Now, what I would like to have is splitter automatically fixed on position when everything on the left (treeview) is visible and horizontal scroll bar is not displayed. I don't know in advance the size of the items (but I know that it's only 1 level deep). Something like "AutoGrow and then fix splitter". Surely I'm not the first one that is looking for this functionality :)

Any idea how to achieve this?

Thanks, Martin

Was it helpful?

Solution

This is fairly troublesome, the scrollbar may appear and disappear again as the user expands and collapses nodes. You can only measure the size of an expanded node. Which is however likely the case in the kind of UI you are using. In which case this code ought to solve your problem:

Private Shared Function GetMaxNodeWidth(ByVal nodes As TreeNodeCollection, ByVal width As Integer) As Integer
    For Each node As TreeNode In nodes
        width = Math.Max(width, node.Bounds.Right)
        width = GetMaxNodeWidth(node.Nodes, width)
    Next
    Return width
End Function

Public Shared Function ResizeTreeView(ByVal tree As TreeView) As Integer
    Dim width = GetMaxNodeWidth(tree.Nodes, 0)
    tree.ClientSize = New Size(width, tree.ClientSize.Height)
    return tree.Width
End Sub

Call ResizeTreeView() after you populated the control. Sample usage:

    TreeView1.Nodes.Add("Customers")
    '' etc...
    SplitContainer1.SplitterDistance = ResizeTreeView(TreeView1)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top