1

我正在开发一个用于模拟串行键盘的简单应用程序。该应用程序需要有一个可调整大小的窗口,并且“键”(在本例中为按钮)随窗口伸展。我决定使用停靠的 TableLayoutPanel 并使用百分比设置列和行的大小。

假设我的键盘上有一个 7 x 3 的虚拟键网格,如下所示:

+--+--+--+--+--+--+--+
|  |  |  |  |  |  |  |
+--+--+--+--+--+--+--+
|  |  |  |  |  |  |  |
+--+--+--+--+--+--+--+
|  |  |  |  |  |  |  |
+--+--+--+--+--+--+--+

当我调整表单大小时,当空间无法平均分配时,最后一行和最后一列会占用空间。

因为我要在每个单元格中停靠一个按钮,所以当最后一行/最后一列按钮较大时,它看起来很奇怪。

我通过简单地添加一个没有任何按钮的额外行和列来解决这个问题,但是当所有的松弛空间都在右侧和底部时,这看起来很丑,我真正想要的是基本上使键盘居中。

我尝试创建一个 9 x 5 网格并将第一列和最后一列和行设置为自动调整大小,但我似乎无法实现我想要实现的目标。

一定有办法做到这一点?

编辑:这是我的设计器代码。我实际上是在模拟一个 19 x 10 网格的键盘。

this.tlpKeyboard1.ColumnCount = 20;
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 5F));
this.tlpKeyboard1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tlpKeyboard1.Location = new System.Drawing.Point(3, 3);
this.tlpKeyboard1.Name = "tlpKeyboard1";
this.tlpKeyboard1.RowCount = 11;
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 9.090908F));
this.tlpKeyboard1.Size = new System.Drawing.Size(977, 528);
4

1 回答 1

2

设计器中的这段代码对我来说似乎很好用。它有 3 列和 2 行(每个区域有一个按钮),并且在我展开和收缩窗口时会正确自动调整大小。也许您可以将其与您所拥有的进行比较:

     // 
     // tableLayoutPanel1
     // 
     this.tableLayoutPanel1.ColumnCount = 3;
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
     this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33333F));
     this.tableLayoutPanel1.Controls.Add(this.button9, 2, 1);
     this.tableLayoutPanel1.Controls.Add(this.button8, 1, 1);
     this.tableLayoutPanel1.Controls.Add(this.button7, 0, 1);
     this.tableLayoutPanel1.Controls.Add(this.button6, 1, 0);
     this.tableLayoutPanel1.Controls.Add(this.button5, 0, 0);
     this.tableLayoutPanel1.Controls.Add(this.button10, 2, 0);
     this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
     this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
     this.tableLayoutPanel1.Name = "tableLayoutPanel1";
     this.tableLayoutPanel1.RowCount = 2;
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
     this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
     this.tableLayoutPanel1.Size = new System.Drawing.Size(292, 266);
     this.tableLayoutPanel1.TabIndex = 6;
于 2011-02-17T12:40:02.280 回答