Click here to Skip to main content
15,077,377 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to resize tablelayoutpanel control during runtime in c#?

How can I achieve this? .
Posted
Comments
LanFanNinja 25-Nov-11 1:27am
   
Check my new solution (Solution 2) below
d.allen101 2-Jul-13 16:31pm
   
hey Srini2286, did you ever get that tableLayoutPanel working with the starting code that LanFanNinja provided? If you did can plz share? i'm dealing with the exact same problem. i need for the user of my app to be able to resize rows and columns of my tableLayoutPanel during runtime.

Well its not going to be easy for you to do but I come up with this code to maybe give you an idea of something you could do as far as resizing goes anyway.

C#
public partial class Form1 : Form
{
    bool resizing = false;
    TableLayoutRowStyleCollection rowStyles;
    TableLayoutColumnStyleCollection columnStyles;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rowStyles = tableLayoutPanel1.RowStyles;
        columnStyles = tableLayoutPanel1.ColumnStyles;
    }

    private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            resizing = true;
        }
    }

    private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (resizing)
        {
            columnStyles[0].SizeType = SizeType.Absolute;
            rowStyles[0].SizeType = SizeType.Absolute;
            rowStyles[0].Height = e.Y;
            columnStyles[0].Width = e.X;
        }
    }

    private void tableLayoutPanel1_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            resizing = false;
        }
    }
}


EDIT: I also set the CellBorderStyle propery to Single so that I could see the lines
C#
tableLayoutPanel1.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
   
v4
Using the Size propery i.e.
C#
tableLayoutPanel1.Size = new Size(100, 100);


Is this what you wanted?
   
Comments
Srini2286 24-Nov-11 23:06pm
   
No. Actually I wanted to resize tablelayout during runtime.. That is,drag and drop the rows and columns at runmode according to user desire..

Need help for this...
LanFanNinja 25-Nov-11 1:40am
   
Check my solution 2
My answer is for using the mouse to resize only the columns of the tablelayoutpanel but can be easily updated to do the same for rows. I'd rather use a splitpanel for the rows of course, but still. In the code I cater for 4 columns but extending it to more or less is easy enough. The cursurToDefault event will be added to the mousemove-event of the container controls inside the tablelayoutpanel.

C#
private int beamMoving;
private void tableLayoutPanel1_MouseMove(object sender, MouseEventArgs e)
   {
       this.Cursor = Cursors.VSplit;
       if (e.Button == System.Windows.Forms.MouseButtons.Left)
       {
           int beam0 = (int)tableLayoutPanel1.ColumnStyles[0].Width;
           int beam1 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width;
           int beam2 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width + (int)tableLayoutPanel1.ColumnStyles[2].Width;

           switch (beamMoving)
           {
               case 0:
                   {
                       if (e.X > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[0].Width = e.X;
                       }
                       break;
                   }
               case 1:
                   {
                       if (e.X - beam0 > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[1].Width = e.X - beam0;
                       }
                       break;
                   }
               case 2:
                   {
                       if (e.X - beam1 > 0)
                       {
                           tableLayoutPanel1.ColumnStyles[2].Width = e.X - beam1;
                       }
                       break;
                   }
           }
       }
   }
   private void cursorToDefault(object sender, MouseEventArgs e)
   {
       this.Cursor = Cursors.Default;
   }

   private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
   {
       int beam0 = (int)tableLayoutPanel1.ColumnStyles[0].Width;
       int beam1 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width;
       int beam2 = (int)tableLayoutPanel1.ColumnStyles[0].Width + (int)tableLayoutPanel1.ColumnStyles[1].Width + (int)tableLayoutPanel1.ColumnStyles[2].Width;
       if (e.X + 20 > beam0 && e.X - 20 < beam1)
       {
           beamMoving = 0;
       }
       if (e.X + 20 > beam1 && e.X - 20 < beam2)
       {
           beamMoving = 1;
       }
       if (e.X + 20 > beam2 && e.X - 20 < tableLayoutPanel1.Width)
       {
           beamMoving = 2;
       }
   }
   

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