Search code examples
vb.netautosizeflowlayoutpanelsplitcontainer

Splitcontainer flowlayoutpanel or autosized panel (VB.NET)


Forms in an application I'm working on with a team have a datagridview as main component (it should take up most of the size), but there are other components. there is a horizontal splitcontainer to split them, but I was wondering how to make the top panel resize to its contents. Unfortunately, the panels in a splitcontainer don't have an AutoSize property...

Here are two images to show what we need: image1
(source: mediafire.com)

image2
(source: mediafire.com)

As you can see, the top panel of the splitcontainer should adjust to the size of its contents. Is there any way to achieve this?


Solution

  • I'm assuming you meant "horizontal" split container based on your image.

    You can try achieving this manually by using the ControlAdded event of the top panel:

    Public Class Form1
    
      Public Sub New()
        InitializeComponent()
      End Sub
    
      Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        SplitContainer1.SplitterDistance = SmallPanel.Height
      End Sub
    
      Private Sub SplitContainer1_Panel1_ControlAdded(ByVal sender As Object, ByVal e As ControlEventArgs) Handles SplitContainer1.Panel1.ControlAdded
        SplitContainer1.SplitterDistance += e.Control.Height
      End Sub
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim addPanel As New Panel
        addPanel.BorderStyle = BorderStyle.FixedSingle
        addPanel.Size = New Size(SplitContainer1.Panel1.ClientSize.Width, 100)
        addPanel.Location = New Point(0, SplitContainer1.SplitterDistance)
        addPanel.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or AnchorStyles.Right
        SplitContainer1.Panel1.Controls.Add(addPanel)
      End Sub
    
    End Class
    

    SmallPanel is a panel I placed in Panel1 of the SplitContainer and added a button in their to add more panels.