首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Flowlayoutpanel中设置控件,调整大小并使用可用空间?

如何在Flowlayoutpanel中设置控件,调整大小并使用可用空间?
EN

Stack Overflow用户
提问于 2017-01-09 14:40:49
回答 2查看 1K关注 0票数 0

我有以下屏幕:

我想要做的是:

当第二个“隐藏”RichTextBox设置为RichTextbox2.Visible = false时,如何才能让第一个RichTextBox和第三个RichTextBox扩展FlowLayoutPanel

这样做的目的是让FlowLayoutPanel中可见的任何控件在从FlowLayoutPanel中加载数据时填补空间,而这些数据并不像图2所示的那样在FlowLayoutPanel中使用。因此,如果有另一个RichTextBox,那么所有3个都将占用FlowLayoutPanel中的所有可用空间。

我尝试了以下的建议here,但我无法正确地得到扩展的未使用的空间。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-09 16:24:50

应该很简单的数学..。(这里没有任何效率)

代码语言:javascript
运行
复制
'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像素高的文本框.

票数 1
EN

Stack Overflow用户

发布于 2017-01-09 16:54:57

您可能希望使用TableLayoutPanel来代替:

代码语言:javascript
运行
复制
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

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

代码语言:javascript
运行
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41550427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档