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)