简体   繁体   English

SplitContainer面板自动适应内容

[英]SplitContainer panel to fit content automatically

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. 我有两个面板的拆分容器 - 非常简单的设置,左侧的导航TreeView,右侧的项目ListView。

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). 我事先并不知道物品的大小(但我知道它只有1层深)。 Something like "AutoGrow and then fix splitter". 像“AutoGrow然后修复分离器”之类的东西。 Surely I'm not the first one that is looking for this functionality :) 当然我不是第一个正在寻找这个功能的人:)

Any idea how to achieve this? 知道怎么做到这一点?

Thanks, Martin 谢谢,马丁

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. 然而,在您使用的UI类型中可能存在这种情况。 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. 填充控件后调用ResizeTreeView()。 Sample usage: 样品用法:

    TreeView1.Nodes.Add("Customers")
    '' etc...
    SplitContainer1.SplitterDistance = ResizeTreeView(TreeView1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM