我在用户控件中添加了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时,似乎会导致列大小调整问题。

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

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

在专门设置AutoSize=false的位置创建自己的TextBox可以解决此问题:

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

关于c# - TableLayoutPanel中的列大小在设计器中不断变化,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9946994/

10-11 22:45
查看更多