Search code examples
c#resizedesignertablelayoutpanel

column size in TableLayoutPanel keeps changing in designer


I added a tableLayoutPanel to my user control.

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

The size settings for the columns are as follows.

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

As I understand it the absolute values should always remain the correct size and then column 1 should take up the rest of the space.

When I initially create the control everything looks fine. Ill go back to coding some other area of my program doesn't mater what, and at some point ill return to the designer where my tableLayoutPanel is and the fourth column will be larger then before. So I have to open up the Column and Row Styles editor and change the size Column for back to 25.

How can I get these settings to stick? It is driving me crazy!

As Requested here is my designer code.

        // 
        // 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;

Solution

  • Not 100% sure, but I was able to duplicate the problem once I placed labels in the TableLayoutPanel.

    When the Label's AutoSize=true were set, it seemed to cause the column resize problem.

    When I changed all of the Label's AutoSize=false, the problem went away, and the column sizes stayed the same.

    The TextBoxes also interfere because they have an AutoSize property (not visible in designer or from code) to control the height of the control. Setting the Textbox MultiLine=true did not help.

    Creating my own TextBox where I specifically set the AutoSize=false fixed the problem:

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