如何让 Flowlayoutpanel 内的控件调整大小并使用可用 space?

How to have controls inside Flowlayoutpanel resize and use available space?

我有以下屏幕:

我希望发生的事情如下:

当第 2 个 "hidden" RichTextBox 设置为 RichTextBox 和第 3 个 RichTextBox 时,如何扩展 FlowLayoutPanel RichTextbox2.Visible = false.

想法是让 FlowLayoutPanel 中可见的任何控件在从 [=13] 中未使用的数据库加载数据时填充 space =] 如图 2 所示。因此,如果还有另一个 RichTextBox,则所有 3 个都将占用 FlowLayoutPanel.

内所有可用的 space

我已经尝试了以下建议,但我无法获得扩展未使用的space。

应该是相当简单的数学...(这里不追求任何效率)

'assuming you may have a variable number of richtextboxes you need to get a count of the ones that are visible
'also assuming the richtextboxes are already children of the flowlayoutpanel
'call this sub after you have put the unsized richtextboxes into the FlowlayoutPanel (assuming you are doing that dynamically)    
Private Sub SizeTextBoxes()
    Dim Items As Integer = 0
    'create an array for the richtextboxes you will be sizing
    Dim MyTextBoxes() As RichTextBox = Nothing
    For Each Control As Object In FlowLayoutPanel1.Controls
        If TryCast(Control, RichTextBox).Visible Then
            'create a reference to each visible textbox for sizing later
            ReDim Preserve MyTextBoxes(Items)
            MyTextBoxes(Items) = DirectCast(Control, RichTextBox)
            Items += 1
        End If
    Next

    'if the flowlayoutpanel doesn't have any richtextboxes in it then MyTextBoxes will be nothing
    If Not IsNothing(MyTextBoxes) Then
        'get the height for the text boxes based on how many there are and the height of the flowlayoutpanel
        Dim BoxHeight As Integer = FlowLayoutPanel1.Height \ Items
        For Each TextBox As RichTextBox In MyTextBoxes
            TextBox.Height = BoxHeight
        Next
    End If
End Sub

如果 richtextboxes 的数量确实是可变的 - 你可能想设置一个限制,这样你就不会得到 600 个 1 像素高的文本框...

您可能想改用 TableLayoutPanel:

Private WithEvents tlp As New TableLayoutPanel

Public Sub New()
  InitializeComponent()

  tlp.Location = New Point(150, 16)
  tlp.Size = New Size(Me.ClientSize.Width - 166, Me.ClientSize.Height - 32)
  tlp.Anchor = AnchorStyles.Left Or AnchorStyles.Top Or 
               AnchorStyles.Right Or AnchorStyles.Bottom
  tlp.ColumnCount = 1
  tlp.RowCount = 3
  tlp.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100))
  tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 50))
  tlp.RowStyles.Add(New RowStyle(SizeType.Absolute, 32))
  tlp.RowStyles.Add(New RowStyle(SizeType.Percent, 50))

  tlp.Controls.Add(New RichTextBox With {.Dock = DockStyle.Fill}, 0, 0)
  tlp.Controls.Add(New RichTextBox With {.Dock = DockStyle.Fill}, 0, 1)
  tlp.Controls.Add(New RichTextBox With {.Dock = DockStyle.Fill}, 0, 2)

  Me.Controls.Add(tlp)
End Sub

然后隐藏中间行,切换高度:

If tlp.RowStyles(1).Height = 0 Then
  tlp.GetControlFromPosition(0, 1).Enabled = True
  tlp.RowStyles(1).Height = 32
Else
  tlp.GetControlFromPosition(0, 1).Enabled = False
  tlp.RowStyles(1).Height = 0
End If