简体   繁体   中英

TableLayoutPanel Height Property not working

        TableLayoutPanel t = new TableLayoutPanel();  
        t.RowStyles.Add(new RowStyle(SizeType.AutoSize));
        t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100));
        t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
        Label lbl = new Label();
        lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20);
        lbl.Text = "Hello";
        t.Controls.Add(lbl, 0, 0);
        this.Text = t.Size.Height.ToString();
        this.Controls.Add(t);

Why the t.Size.Height property gives me 100 always ?

The reason this always was returning 100 is that you need:

  • AutoSize = true
  • AutoSizeMode =AutoSizeMode.GrowAndShrink
  • t.RowCount >= 1
  • t.ColumnCount >= 1

      TableLayoutPanel t = new TableLayoutPanel(); t.AutoSize = true; //added t.AutoSizeMode =AutoSizeMode.GrowAndShrink; //added t.RowCount = 1; //added t.ColumnCount = 1; //added t.RowStyles.Add(new RowStyle(SizeType.AutoSize)); t.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100)); t.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; Label lbl = new Label(); lbl.Margin = new System.Windows.Forms.Padding(20, 150, 20, 20); lbl.Text = "Hello"; t.Controls.Add(lbl, 0, 0); this.Controls.Add(t); this.Text = t.Size.Height.ToString(); //moved 

You also need to move your Height check to after you have added the Table to the form, else no layout operations will have happened.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM