如何使 TableLayoutPanel 中的所有列自动调整为完全相同的宽度?

发布于 2024-07-18 14:13:16 字数 9825 浏览 9 评论 0原文

我正在尝试创建一个相当广泛的用户控件,将方形控件合并到设计中,并允许调整大小。 因为设计需要正方形,所以我需要 TableLayoutPanel 中的所有列具有相同的宽度,以便包含的、停靠的控件也是正方形的。

不幸的是,TableLayoutPanel 的行为没有给我这个结果。
使用将所有列设置为使用相同百分比的控件的 TableLayoutPanel 会产生(在一组 7 列中)6 列等宽,以及第 7 列宽度可变。
我理解发生这种行为的原因是,对于每 7 个尺寸中的 6 个尺寸,在 7 列周围共享的列像素数量不相等,而第 7 列是这种不等式的溢出。

我想我想要的是像第 8 列这样的东西,它接受其他 7 列的溢出,允许所有 7 个“真实”列具有真正相等的宽度,但允许第 8 列的宽度为 0 .
到目前为止,我找不到允许这种行为的设置。

谁能告诉我如何让 TableLayoutPanel 执行我想要的操作,或者我是否必须开始编写大量解决方法代码?

编辑:

为了响应 Yacoder 的回答,我添加了一些代码来演示该问题,而另一个代码则显示了使用 TableLayoutPanel 和 Dock 属性的标准功能解决该问题的不成功、天真的尝试问题

演示:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 7
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 7
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

天真的解决方案:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 8
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 8
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

我对这段代码在重绘时的性能非常糟糕。

I'm trying to create a fairly extensive UserControl incorporating square controls into the design, and allowing resizing.
Because the design calls for squares, I need all the columns in my TableLayoutPanels to be the same width, so that the contained, docked controls are also square.

Unfortunately, the behaviour of the TableLayoutPanel does not give me this result.
Using a TableLayoutPanel with all columns set to use the same percentage of the Control gives (in a set of 7 columns) 6 columns of equal width, and a 7th column which is of variable width.
I understand that this behaviour occurs because for 6 out of every 7 sizes, there are not an equal number of column-pixels to share around the 7 columns, and the 7th column is the overflow for this inequality.

What I guess I want is something like an 8th column that takes the overflow of the other 7 columns, allowing all 7 of the "real" columns to be of real equal widths, but for the 8th column to be allowed to be of 0 width.
So far, I cannot find a setting which allows this behaviour.

Can anyone tell me how to get the TableLayoutPanel to do what I want, or am I going to have to start writing a lot of workaround code?

EDIT:

In response to Yacoder's answer, I have added some code which demonstrates the problem, and another which shows an unsuccessful, naive attempt at solving it using standard features of the TableLayoutPanel and the Dock property

Problem demonstration:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 7
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 7
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

Naive solution:

Public Class Form1
Inherits System.Windows.Forms.Form
Public Sub New()
    Me.InitializeComponent()
End Sub
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
    Try
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
    Finally
        MyBase.Dispose(disposing)
    End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.  
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel
    Me.SuspendLayout()
    '
    'TableLayoutPanel1
    '
    Me.TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
    Me.TableLayoutPanel1.ColumnCount = 8
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28572!))
    Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
    Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
    Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
    Me.TableLayoutPanel1.RowCount = 8
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 14.28571!))
    Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 0.0!))
    Me.TableLayoutPanel1.Size = New System.Drawing.Size(261, 264)
    Me.TableLayoutPanel1.TabIndex = 0
    '
    'Form1
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.ClientSize = New System.Drawing.Size(261, 264)
    Me.Controls.Add(Me.TableLayoutPanel1)
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

End Sub
Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
Private labelList As List(Of Label)
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
    MyBase.OnLoad(e)
    labelList = New List(Of Label)
    For JJ As Integer = 0 To Me.TableLayoutPanel1.ColumnCount - 1
        For II As Integer = 0 To Me.TableLayoutPanel1.RowCount - 1
            Dim addLabel As New Label
            Me.TableLayoutPanel1.Controls.Add(addLabel, JJ, II)
            addLabel.Dock = DockStyle.Fill
            Me.labelList.Add(addLabel)
        Next
    Next
End Sub
Private Sub TableLayoutPanel1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles TableLayoutPanel1.Resize
    If Me.labelList IsNot Nothing Then
        For Each labelIn As Label In Me.labelList
            labelIn.Text = labelIn.Width.ToString & ", " & labelIn.Height.ToString
        Next
    End If
End Sub
End Class

My apologies for the abysmal performance of this code on redrawing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼眸里的快感 2024-07-25 14:13:16

也许我不太明白这个问题,但是......这就是我所做的:

  1. 创建新表单
  2. 在表单上放置一个TableLayoutPanel,然后设置Dock = Fill
  3. In Form设计窗口(如果您使用的是VS)单击小箭头在 TableLayoutPanel 上打开一个包含任务的菜单,然后转到“编辑行和列...”,
  4. 在该窗口中添加所需数量的列,并将所有列设置为整体大小的“百分比”,并在各处放置相同的数字。 你可以输入任意数字,只要保证相同即可,VS会自动使总和等于100%。

而且......它应该可以工作,至少当我调整这样的表单大小时,我看到所有列一起调整大小......

如果你不在VS中,或者对该方法有麻烦,这是我在设计器中自动生成的代码班级:

    this.tableLayoutPanel1.ColumnCount = 7;
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));

Maybe I didn't quite understand the question, but... that's what I did:

  1. Create new form
  2. Drop a TableLayoutPanel on the form, and set Dock = Fill
  3. In Form design window (if you are using VS) click on the small arrow on TableLayoutPanel to open a menu with tasks and go to "Edit Rows and Columns..."
  4. In that window add as many columns as you need and set all of them to take "Percent" of the whole size and put the same number everywhere. You can put any number, just ensure it is the same, VS will make the sum equal to 100% automatically.

And... it should work, at least when I resize such form I see all columns resized together...

If you are not in VS, or have troubles with that method, this is the code I have auto-generated in my Designer class:

    this.tableLayoutPanel1.ColumnCount = 7;
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
    this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 14.28571F));
眼睛会笑 2024-07-25 14:13:16

我尝试在我的环境中进行设置,并阅读您的帖子大约 6 或 7 次,但我无法确定您到底想要做什么。 我有 6 列已停靠的 SWF GroupBox 对象,停靠以填充设置为相同%宽度的列,以便我可以轻松地看到它们调整大小。 我还将第七列设置为 0px 绝对宽度以用作溢出。 这正如我所期望的那样。

我无法确定此设置有什么问题和/或您试图让它做什么。 您能更好地描述一下您需要 TableLayoutPanel 做什么吗?

I have tried to set this up in my environment as well as reading your post about 6 or 7 times and I can't determine what it is exactly that you are trying to do. I have 6 columns that have docked SWF GroupBox objects docked to fill with the columns set to the same % width so I can easily see them resize. I also have a 7th column set to 0px absolute width to work as an overflow. This works as I would expect it to.

What I can't determine is what is wrong with this setup and/or what you are trying to get it to do. Can you please describe better what you need the TableLayoutPanel to do?

早乙女 2024-07-25 14:13:16
public MainForm()
{
    TableLayoutPanel pnlDragAndDrop = new TableLayoutPanel();

    // make the panel full width
    pnlDragAndDrop.Dock = DockStyle.Fill;

    // be sure to add columns and rows explicitly!
    pnlDragAndDrop.ColumnCount = 2;
    pnlDragAndDrop.RowCount = 1;

    // add a border just for testing
    pnlDragAndDrop.CellBorderStyle = 
        TableLayoutPanelCellBorderStyle.InsetDouble;
    pnlDragAndDrop.CellPaint += 
        new TableLayoutCellPaintEventHandler(TblLayoutPanel_CellPaint);

    // add a column style for each column!
    for (int i = 0; i < pnlDragAndDrop.ColumnCount * pnlDragAndDrop.RowCount; ++i)
    {
        pnlDragAndDrop.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
    }

    // add the panel to the form
    this.Controls.Add(pnlDragAndDrop);
}

private void TblLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    // Add a border around each cell
    e.Graphics.DrawLine(
        Pens.Black, 
        e.CellBounds.Location, 
        new Point(e.CellBounds.Right, e.CellBounds.Top)
        );
}
public MainForm()
{
    TableLayoutPanel pnlDragAndDrop = new TableLayoutPanel();

    // make the panel full width
    pnlDragAndDrop.Dock = DockStyle.Fill;

    // be sure to add columns and rows explicitly!
    pnlDragAndDrop.ColumnCount = 2;
    pnlDragAndDrop.RowCount = 1;

    // add a border just for testing
    pnlDragAndDrop.CellBorderStyle = 
        TableLayoutPanelCellBorderStyle.InsetDouble;
    pnlDragAndDrop.CellPaint += 
        new TableLayoutCellPaintEventHandler(TblLayoutPanel_CellPaint);

    // add a column style for each column!
    for (int i = 0; i < pnlDragAndDrop.ColumnCount * pnlDragAndDrop.RowCount; ++i)
    {
        pnlDragAndDrop.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50F));
    }

    // add the panel to the form
    this.Controls.Add(pnlDragAndDrop);
}

private void TblLayoutPanel_CellPaint(object sender, TableLayoutCellPaintEventArgs e)
{
    // Add a border around each cell
    e.Graphics.DrawLine(
        Pens.Black, 
        e.CellBounds.Location, 
        new Point(e.CellBounds.Right, e.CellBounds.Top)
        );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文