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

1.3k views Asked by At

I have the following screen:

enter image description here

What I would like to happen is the following:enter image description here

How will it be possible to have the first RichTextBox and the 3rd RichTextBox expand the FlowLayoutPanel when the 2nd "hidden" RichTextBox is set to RichTextbox2.Visible = false.

The idea is to have any of the controls, that are visible inside the FlowLayoutPanel to fill up the space when loading data from the database that is not getting used inside the FlowLayoutPanel as image 2 suggests. So if there is another RichTextBox that all 3 will then occupy all available space inside the FlowLayoutPanel.

I have tried the following suggestions here, but I can't get the expanding unused space right.

2

There are 2 answers

0
Destek On BEST ANSWER

Should be fairly simple math... (not going for any efficiency here)

'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

If the number of richtextboxes is indeed variable - you may want to put a limit so you don't wind up with 600 1-pixel high text boxes...

0
LarsTech On

You probably want to use a TableLayoutPanel instead:

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

Then to hide the middle row, toggle the height:

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