我正在开发一个用于模拟串行键盘的简单应用程序。该应用程序需要有一个可调整大小的窗口,并且“键”(在本例中为按钮)随窗口伸展。我决定使用停靠的 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);