Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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:
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 29-Mar-13 0:24am
Edited 23-Apr-13 22:57pm
lukeer15K
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

this.text.Anchor = System.Windows.Forms.AnchorStyles.None;
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

change tablelayoutpanel properties dock style fill
  Permalink  
Comments
lukeer at 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)

  Print Answers RSS
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,192
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 21 Dec 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100