2017-01-09 77 views
0

我有以下畫面:如何讓Flowlayoutpanel內的控件調整大小並使用可用空間?

enter image description here

我想什麼發生的是以下幾點:enter image description here

怎麼會可能有第一RichTextBox和第三RichTextBox擴大FlowLayoutPanel時第二個「隱藏」RichTextBox設置爲RichTextbox2.Visible = false

這個想法是在FlowLayoutPanel內部可見的任何控件填充空間,當從數據庫加載數據時,如圖2所示,在FlowLayoutPanel內部沒有使用數據。所以如果有另一個RichTextBox,那麼所有3個將佔用FlowLayoutPanel內的所有可用空間。

我已經嘗試了以下建議here,但我無法獲得擴大的未使用空間。

+0

我建議你只是使用標準面板,自己做數學。或者也許使用分離器控制? – rheitzman

回答

1

應該是相當簡單的數學......(不會對任何效率這裏)

'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像素高的文本框...

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