我正在开发一个用于模拟串行键盘的简单应用。该应用程序需要一个可调整大小的窗口,其中“键”(在这种情况下为按钮)与窗口一起拉伸。我决定使用停靠的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);
答案 0 :(得分: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;