Click here to Skip to main content
15,903,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell me how to align tablelayoutpanel to start from center of screen. I have a tablelayoutpanel in which i have added few buttons dynamically & are aligned in rows & columns. These buttons are added by user, like the user can give the no. of buttons to be added in the tablelayoutpanel. So based on the no.of buttons added tablelayoutpanel keeps growing. What i am interested in is that this tablelayoutpanel should start form center & equally divide in all the directions.

or


I am creating a TableLayoutPanel in a C# program at run-time. How can I center it and dynamically resize it on a form depending on how many rows/columns I add?

[Edit]
Here is some code:
C#
private void GenerateTable(int columnCount, int rowCount,string allignment)
{
    int count=0;
    int width = 0;
    int height = 0;
    int btncount = 1;

    //Clear out the existing controls, we are generating a new table layout
    tableLayoutPanel1.Controls.Clear();

    //Clear out the existing row and column styles
    tableLayoutPanel1.ColumnStyles.Clear();
    tableLayoutPanel1.RowStyles.Clear();

    //Now we will generate the table, setting up the row and column counts first
    tableLayoutPanel1.ColumnCount = columnCount;
    tableLayoutPanel1.RowCount = rowCount;



#region AllignMentofButtonIntableLayoutPanel

    for (int x = 0; x < columnCount; x++)
    {
        if (x == 0)
        {
            tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
        }
        width += 120;
        ////First add a column

        for (int y = 0; y < rowCount; y++)
        {
            //Next, add a row.  Only do this when once, when creating the first column
            if (x == 0)
            {
                tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
            }
            pnelLayout[count] = new Panel();
            btn[count] = new Button();
            btn[count].Height = 60;
            btn[count].Width = 120;
            btn[count].Text = data[count];
            btn[count].Name = count.ToString();
            btn[count].Height = 60;
            btn[count].Width = 120;
            pnelLayout[count].Width = 120;
            pnelLayout[count].Height = 60;
            //Get the btnName from Configuration windows and check the btnName with Description
            if (dtButtonName.Rows.Count > 0)
            {
                //Check for the same btn text Name,if it is true change the colour
                for (int i = 0; i < dtButtonName.Rows.Count; i++)
                {
                    //Check for btnName and btnCount Index
                    if (data[count] == dtButtonName.Rows[i]["BtnName"].ToString() && (btncount - 1).ToString() == dtButtonName.Rows[i]["BtnIndex"].ToString())
                        btn[count].BackColor = Color.WhiteSmoke;
                    else if (btn[count].BackColor == Color.WhiteSmoke)
                    {
                    }
                    else
                        btn[count].BackColor = Color.LightGreen;
                }
            }
            else
            {
                btn[count].BackColor = Color.LightGreen;
            }

            btn[count].TextAlign = ContentAlignment.MiddleCenter;
            btn[count].FlatStyle = FlatStyle.Popup;
            btn[count].MouseDown += new MouseEventHandler(Annaunciator_Window_MouseDown);
            btn[count].ContextMenuStrip = contextMenuStrip1;
            pnelLayout[count].Controls.Add(lblCorner[count]);
            pnelLayout[count].Controls.Add(btn[count]);
        }
    }
#endregion
}

[/Edit]
Posted
Updated 23-Apr-13 21:57pm
v3

this.text.Anchor = System.Windows.Forms.AnchorStyles.None;
 
Share this answer
 
change tablelayoutpanel properties dock style fill
 
Share this answer
 
Comments
lukeer 24-Apr-13 3:59am    
Code belongs to the question. Wrap it in <pre lang="c#">tags</pre> there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900