我有以下屏幕:
我想要做的是:
当第二个“隐藏”RichTextBox
设置为RichTextbox2.Visible = false
时,如何才能让第一个RichTextBox
和第三个RichTextBox
扩展FlowLayoutPanel
。
这样做的目的是让FlowLayoutPanel
中可见的任何控件在从FlowLayoutPanel
中加载数据时填补空间,而这些数据并不像图2所示的那样在FlowLayoutPanel
中使用。因此,如果有另一个RichTextBox
,那么所有3个都将占用FlowLayoutPanel
中的所有可用空间。
我尝试了以下的建议here,但我无法正确地得到扩展的未使用的空间。
发布于 2017-01-09 16:24:50
应该很简单的数学..。(这里没有任何效率)
'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
如果丰富文本框的数量确实是可变的-您可能需要设置一个限制,这样您就不会有600个1像素高的文本框.
发布于 2017-01-09 16:54:57
您可能希望使用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
https://stackoverflow.com/questions/41550427
复制相似问题