Add control at the last in tablelayoutpanel c#

Hemanth B 886 Reputation points
2022-02-26T16:00:27.147+00:00

When I add a control to a tablelayout panel, it inserts the control at the beginning of the panel instead of at the last.

              tableLayoutPanel1.Controls.Add(mTAButtonRounded, tableLayoutPanel1.ColumnCount + 1, 0);
                tableLayoutPanel1.ColumnStyles.Clear();
                for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
                {
                    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                }
    
    
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,392 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 82,116 Reputation points
    2022-02-26T17:48:06.407+00:00

    If I do a test, the Button is inserted in last column/last row =>

    178142-tablelayoutpanel.jpg

        private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;  
        private System.Windows.Forms.Button button1;  
    

    then :

            tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();  
            
            tableLayoutPanel1.ColumnCount = 3;  
            tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33F));  
            tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33F));  
            tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 33.33F));  
            tableLayoutPanel1.Location = new System.Drawing.Point(10, 10);  
            tableLayoutPanel1.Name = "tableLayoutPanel1";  
            tableLayoutPanel1.RowCount = 5;  
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));  
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));  
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));  
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));  
            tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 20F));  
            tableLayoutPanel1.Size = new System.Drawing.Size(400, 200);  
            tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Outset;  
    
            button1 = new System.Windows.Forms.Button();  
            button1.Name = "button1";  
            button1.Text = "button1";  
            button1.UseVisualStyleBackColor = true;  
            tableLayoutPanel1.Controls.Add(button1, tableLayoutPanel1.ColumnCount -1, tableLayoutPanel1.RowCount -1);  
    
            Controls.Add(tableLayoutPanel1);  
    
    0 comments No comments