TableLayoutPanel中的列大小在设计器中不断变化

时间:2012-03-30 16:35:10

标签: c# resize designer tablelayoutpanel

我在我的用户控件中添加了一个tableLayoutPanel。

It is not docked or anchored.
It has 4 columns and 4 rows.
The items within each cell are docked fully within each cell.

列的大小设置如下。

-Column 1 - Percent - 100%
-Column 2 - Absolute - 25
-Column 3 - Absolute - 35
-Column 4 - Absolute - 25

据我了解,绝对值应始终保持正确的大小,然后第1列应占用剩余的空间。

当我最初创建控件时,一切看起来都很好。我会回到编码我的程序的其他一些区域并不会改变什么,并且在某些时候生病了返回到我的tableLayoutPanel所在的设计器,第四列将比之前更大。所以我必须打开Column and Row Styles编辑器并将大小Column更改为25.

如何才能让这些设置坚持下去?这让我发疯了!

如此处所要求的是我的设计师代码。

        // 
        // tableLayoutPanel1
        // 
        this.tableLayoutPanel1.Anchor = System.Windows.Forms.AnchorStyles.None;
        this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.OutsetDouble;
        this.tableLayoutPanel1.ColumnCount = 4;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 35F));
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 25F));
        this.tableLayoutPanel1.Controls.Add(this.label8, 0, 0);
        this.tableLayoutPanel1.Controls.Add(this.btnStartingUp, 3, 3);
        this.tableLayoutPanel1.Controls.Add(this.txtOffset, 2, 3);
        this.tableLayoutPanel1.Controls.Add(this.btnDurationUp, 3, 2);
        this.tableLayoutPanel1.Controls.Add(this.label10, 0, 1);
        this.tableLayoutPanel1.Controls.Add(this.btnFrequencyUp, 3, 1);
        this.tableLayoutPanel1.Controls.Add(this.txtFrequency, 2, 1);
        this.tableLayoutPanel1.Controls.Add(this.btnPowerUp, 3, 0);
        this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
        this.tableLayoutPanel1.Controls.Add(this.txtLaserPower, 2, 0);
        this.tableLayoutPanel1.Controls.Add(this.label7, 0, 3);
        this.tableLayoutPanel1.Controls.Add(this.btnPowerDown, 1, 0);
        this.tableLayoutPanel1.Controls.Add(this.btnFrequencyDown, 1, 1);
        this.tableLayoutPanel1.Controls.Add(this.btnDurationDown, 1, 2);
        this.tableLayoutPanel1.Controls.Add(this.btnOffsetDown, 1, 3);
        this.tableLayoutPanel1.Controls.Add(this.txtDuration, 2, 2);
        this.tableLayoutPanel1.Location = new System.Drawing.Point(61, 610);
        this.tableLayoutPanel1.Margin = new System.Windows.Forms.Padding(0);
        this.tableLayoutPanel1.MaximumSize = new System.Drawing.Size(208, 98);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.RowCount = 4;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 21F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 21F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 21F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 21F));
        this.tableLayoutPanel1.Size = new System.Drawing.Size(208, 98);
        this.tableLayoutPanel1.TabIndex = 168;

1 个答案:

答案 0 :(得分:2)

不是100%肯定,但是当我在TableLayoutPanel中放置标签时,我能够复制问题。

设置Label AutoSize=true时,似乎会导致列调整大小问题。

当我更改了所有Label的AutoSize=false时,问题就消失了,列大小保持不变。

TextBox还会产生干扰,因为它们具有AutoSize属性(在设计器或代码中不可见)来控制控件的高度。设置文本框MultiLine=true没有帮助。

创建我自己的TextBox,我专门设置AutoSize=false修复了问题:

public class TextBoxEx : TextBox {
  public TextBoxEx() {
    this.AutoSize = false;
  }
}