gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 22:38:36 26 4
gpt4 key购买 nike

我正在尝试创建一个相当广泛的 UserControl,将方形控件合并到设计中,并允许调整大小。
因为设计需要正方形,所以我需要 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

对于这段代码在重绘时的糟糕表现,我深表歉意。

最佳答案

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

  • 创建新表单
  • 在表单上放置一个 TableLayoutPanel,并设置 Dock = Fill
  • 在表单设计窗口(如果您使用 VS)中单击 TableLayoutPanel 上的小箭头以打开带有任务的菜单并转到“编辑行和列...”
  • 在该窗口中,根据需要添加尽可能多的列,并将所有列设置为占整个大小的“百分比”,并在各处放置相同的数字。你可以放任何数字,只要确保它是相同的,VS 会自动使总和等于 100%。

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

    如果您不在 VS 中,或者在使用该方法时遇到问题,这是我在 Designer 类中自动生成的代码:
        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));

    关于.net - 如何使 TableLayoutPanel 中的所有列自动调整为完全相同的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/824012/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com