這是相當麻煩的,滾動條可能會出現,併爲用戶展開和摺疊節點再次消失。您只能測量擴展節點的大小。不過,您在使用的那種用戶界面中可能會出現這種情況。您填寫的控制後
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
呼叫ResizeTreeView():在這種情況下,該代碼應該解決您的問題。樣品用法:
TreeView1.Nodes.Add("Customers")
'' etc...
SplitContainer1.SplitterDistance = ResizeTreeView(TreeView1)