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.
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.
LVL 1
festijazzAsked:
Who is Participating?
 
it_saigeDeveloperCommented:
Why not just use the Anchor properties of the control as opposed to attempting to resize it (I also added a double buffered table layout panel to provide a flicker free experience)?

Form1.cs -
using System;
using System.ComponentModel;
using System.Windows.Forms;

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

		private void OnLoad(object sender, EventArgs e)
		{
			float fRowPercentage;
			float fColumnPercentage;

			Int32 iRows = 8;
			Int32 iCols = 8;

			fColumnPercentage = iCols > 0 ? 100 / iCols : 100;

			fRowPercentage = iRows > 0 ? 100 / iRows : 100;

			//LayoutCtrl.Dock = DockStyle.None;
			LayoutCtrl.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
			LayoutCtrl.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
			LayoutCtrl.ColumnCount = iCols;

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

			LayoutCtrl.RowCount = iRows;

			LayoutCtrl.RowStyles.Clear();
			for (int iRow = 0; iRow < iRows; iRow++)
			{
				LayoutCtrl.RowStyles.Add(new RowStyle(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;
		}
	}

	internal class TableLayoutPanelEx : TableLayoutPanel
	{
		/// <summary>Required designer variable.</summary>
		private IContainer components = null;

		/// <summary>Required method for Designer support - do not modify the contents of this method with the code editor.</summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}

		public TableLayoutPanelEx()
		{
			InitializeComponent();
			SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
		}

		public TableLayoutPanelEx(IContainer container)
		{
			container.Add(this);

			InitializeComponent();
			SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
		}

		/// <summary>Clean up any resources being used.</summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28627557
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.LayoutCtrl = new TableLayoutPanelEx();
			this.SuspendLayout();
			// 
			// LayoutCtrl
			// 
			this.LayoutCtrl.ColumnCount = 2;
			this.LayoutCtrl.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.LayoutCtrl.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.LayoutCtrl.Location = new System.Drawing.Point(13, 13);
			this.LayoutCtrl.Name = "LayoutCtrl";
			this.LayoutCtrl.RowCount = 2;
			this.LayoutCtrl.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.LayoutCtrl.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
			this.LayoutCtrl.Size = new System.Drawing.Size(259, 237);
			this.LayoutCtrl.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 262);
			this.Controls.Add(this.LayoutCtrl);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.OnLoad);
			this.ResumeLayout(false);

		}

		#endregion

		private TableLayoutPanelEx LayoutCtrl;
	}
}

Open in new window

Produces the following output -Inital load before resize.After resize
-saige-
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.