We help IT Professionals succeed at work.
Get Started

Problem to resize the TableLayoutPanel item in runtime or via a preset flag automatically.

425 Views
Last Modified: 2015-10-04
Hello Everyone,

I am trying to autoresize the grid of the TableLayoutPanel when a resize is performed.
When I set the TableLayoutPanel dock to fill it works well but when I set to to none, it does not reset the grid items.

Here is a picture to show you the effect.
tab-grid-issue-1.jpg
Here is the sample lines of code I am using:

   public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            float fRowPercentage = 100;
            float fColumnPercentage = 100;

            Int32 iRows = 8;
            Int32 iCols = 8;

            if (iCols > 0)
                fColumnPercentage = 100 / iCols;

            if (iRows > 0)
                fRowPercentage = 100 / iRows;

            //LayoutCtrl.Dock = DockStyle.None;
            LayoutCtrl.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
            LayoutCtrl.ColumnCount = iCols;

            LayoutCtrl.ColumnStyles.Clear();
            for (int iCol = 0; iCol < iCols; iCol++)
            {
                LayoutCtrl.ColumnStyles.Add(new ColumnStyle(System.Windows.Forms.SizeType.Percent, fColumnPercentage));
            }

            LayoutCtrl.RowCount = iRows;

            LayoutCtrl.RowStyles.Clear();
            for (int iRow = 0; iRow < iRows; iRow++)
            {
                LayoutCtrl.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.Percent, fRowPercentage));
            }

            LayoutCtrl.AutoSize = false;
            LayoutCtrl.GrowStyle = TableLayoutPanelGrowStyle.AddRows;

            LayoutCtrl.SuspendLayout();

            for (int iRow = 0; iRow < iRows; iRow++)
            {
                for (int iCol = 0; iCol < iCols; iCol++)
                {
                    Button btn = new Button();
                    if (btn != null)
                    {
                        // Assign the property changed event.
                        btn.Dock = DockStyle.Fill;
                        LayoutCtrl.Controls.Add(btn);
                    }

                }
            }

            LayoutCtrl.ResumeLayout();
            LayoutCtrl.Update();

            LayoutCtrl.Visible = true;

        }

        private void Form1_Resize(object sender, EventArgs e)
        {

            if (LayoutCtrl == null)
                return;

            LayoutCtrl.Height = this.Height - LayoutCtrl.Top;
            LayoutCtrl.Width = this.Width;
            LayoutCtrl.Update();
            LayoutCtrl.Invalidate();
            LayoutCtrl.Refresh();
        }

    }

Open in new window


Does somebody have an idea to fix that effect ?

Thank you very much in advance.
Best regards.
MiQi.
Comment
Watch Question
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
Unlock 1 Answer and 1 Comment.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE