Link to home
Start Free TrialLog in
Avatar of festijazz
festijazz

asked on

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

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.
User generated image
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.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial