1

Because I'm newbie so I am facing a problem when I want to set value for column width of table (using code C#)

I want to exhibit like picture

enter image description here

But when I code:

    private void Form1_Load(object sender, EventArgs e)
    {
       // TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();

        tableLayoutPanel.ColumnCount = 3;
        tableLayoutPanel.RowCount = 1;

        tableLayoutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;
        tableLayoutPanel.Dock = DockStyle.Top;
        tableLayoutPanel.Height = 100;

        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
        tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));

        #region Create Label1, label2, label3         
        Label label1 = new Label();
        label1.Text = "Width 10%";
        //label1.Dock = DockStyle.Fill;  

        Label label2 = new Label();
        label2.Text = "Width 60%";
        //label1.Dock = DockStyle.Fill;

        Label label3 = new Label();
        label3.Text = "Width 30%";
        //label1.Dock = DockStyle.Fill;
        #endregion 

        tableLayoutPanel.Controls.Add(label1, 0, 0);
        tableLayoutPanel.Controls.Add(label2,1,0);
        tableLayoutPanel.Controls.Add(label3, 2, 0);
    }

The Column width not correct as bellow enter image description here

I don't know why. pls tell me and how to fix it. Thannks for reading

Linh
  • 45
  • 4
  • I don't see where you're adding `tableLayoutPanel` to `this.Controls()` (you're creating a new Control, here). When you do, it'll work as expected :) Unless you have another TableLayoutPanel already in your Form, and you thought you were setting the properties of that one. – Jimi Nov 30 '19 at 04:24
  • How will I write? – Linh Nov 30 '19 at 04:36
  • If that is an existing TableLayoutPanel (as it apperas after your edit), read the notes [here](https://stackoverflow.com/a/54565075/7444103). In practice, you'll have to remove the existing styles first. So, after `tableLayoutPanel.RowCount = 1;`, add `tableLayoutPanel.ColumnStyles.RemoveAt(0);` **twice** (even if, in the designer, there's only one Column and one Row, the Styles are two. It's described in the notes I linked). – Jimi Nov 30 '19 at 04:37
  • Thank Jimi. But sr I don't understand. So, Could you write for me an example? – Linh Nov 30 '19 at 04:52
  • Do you have already, inside your Form1, a TableLayoutPanel named `tableLayoutPanel`? If so, how many Columns/Rows does it have? – Jimi Nov 30 '19 at 04:54
  • Yes, I drag & drop tableLayoutpanel in the form1. And it is tableLayoutPanel, with default: 2 rows and 2 columns – Linh Nov 30 '19 at 05:06
  • Then you have to remove the styles (as also described in LarsTech answer), or remove the TLP you have in the Form's designer and create a new one as you were doing before (with `TableLayoutPanel tableLayoutPanel = new TableLayoutPanel();`). In this case, you don't need to remove the styles. The default style are only created when you drop a TLP object in the Form's designer. – Jimi Nov 30 '19 at 05:08

1 Answers1

0

You probably have existing ColumnStyles already in your collection. Setting the ColumnCount does not reset that collection. Simply clear it out before adding your new ones:

tableLayoutPanel.ColumnStyles.Clear();
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 60F));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F));
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • But, when I dont drag - drop tableLayoutPanel in form. I create `TableLayoutPanel tblpTable = new TableLayoutPanel();` -> Form1 don't show any thing. So Why? – Linh Nov 30 '19 at 05:23
  • @Linh As mentioned, when you drag and drop the control in the designer view, it creates rows and columns for you. When you create it programmatically, like in your comment, it does not. – LarsTech Nov 30 '19 at 17:30