C# TableLayoutPanel中的列大小在designer中不断更改

C# TableLayoutPanel中的列大小在designer中不断更改,c#,resize,designer,tablelayoutpanel,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

我在用户控件中添加了一个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所在的设计器,第四列将比以前更大。因此,我必须打开列和行样式编辑器,将大小列更改为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;

不是100%确定,但我能够在TableLayoutPanel中放置标签后复制问题

设置标签的
AutoSize=true
时,似乎会导致列大小调整问题

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

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

创建我自己的文本框,在其中我专门设置了
AutoSize=false
修复了这个问题:

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

无法复制它。请发布一些这样做的代码。我在设计器中做了所有的事情,它甚至可以在我不运行代码的情况下发生。所以我可以发布我的设计器代码。作为测试,我去表单构造器输入了一些jibberish并删除了它,然后返回到设计器,看到它稍微大一些。。。再一次!似乎没有帮助=(很多时候我回到我的用户控件,最后一列稍微大一点。我肯定缺少了一些愚蠢的设置。我想我可能已经找到了我的用户控件,当我将其更改为“无”时,它承载了TableLayoutPanel的“AutoScaleMode”设置为“Font”这个问题似乎已经消失了。在我决定这是解决方案之前,我会给它一点时间。谢谢你的帮助!@hrh这也是
文本框
,它在高度属性上有一个默认的自动调整大小(属性对你隐藏)。这也是造成问题的原因。好的,我也会更改它,我查找了该属性,但由于它是隐藏的,我认为没有。@hrh这就是为什么我更新了我的答案,以包括一个自定义的
文本框。
。祝你好运。