19

I have a tablelayoutpanel. 2x2 - 2 columns 2 rows.

For example, I added a button button1 in a 1 row, second column. button1 has a dock property set to Fill. VS Designer allows to set column/row span properties of button1.

I want an availability to change row span property of button1 programatically, so it can fill all second column(1 row and second row) and availability to set it back.

How?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Александр Д.
  • 1,119
  • 4
  • 14
  • 23

2 Answers2

33

What about this code?

private void button1_Click(object sender, EventArgs e)
{
    var control = sender as Control;

    if(control == null)
        return;

    if (1 == tableLayoutPanel1.GetRowSpan(control))
    {
        tableLayoutPanel1.SetRowSpan(control, 2);
    }
    else
    {
        tableLayoutPanel1.SetRowSpan(control, 1);
    }
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • 1
    great code. Visual Studio Designer mislead me. I was looking in button1 properties, was trying to cast button1 to toolstippanel item and so on... thanks ! – Александр Д. May 04 '10 at 07:05
  • if you had looked into Form.Designer.cs instead of visual designer you had the solution found directly. ;-) – Oliver May 04 '10 at 07:09
  • 1
    I know it's an old post, but I'd like to point out that you don't need to do the "yoda conditions" in C# since if statements are strongly typed. (And I believe if you do make a mistake with booleans and put an assignment instead of a comparison, it generates a compiler warning, as opposed to the error it generates with any other type.) – MiffTheFox Mar 07 '13 at 17:46
7

While I find the current up-voted answer quite adequate, it also appears slightly messier than need be. You must add the controls to the tableLayoutPanel before setting their properties.

Visual Studio (2013 and likely other versions) will show these properties as part of the control. When in reality, they are part of the tableLayoutPanel.

Explanation:

tableLayoutPanel.Controls.Add(**control**, x, y)
tableLayoutPanel.SetColumnSpan(**control**, '# of cols to span')

Example:

tableLayoutPanel1.Controls.Add(**button1**, 0, 0);
tableLayoutPanel1.SetColumnSpan(**button1**, 2);
tableLayoutPanel1.SetRowSpan(**button1**, 3);

Result: A button which 'occupies' this space. (Provided it is large enough to cover the area. Even if it does not 'cover' the space, it will still 'reserve' it.)

O O X X X
O O X X X
O O X X X
X X X X X
X X X X X

Setting the span larger than the size of the grid will.. : NOT change the grid size. NOT crop/edit the number to the size of the grid. NOT throw an error at compile.

It WILL act/perform as if the span was set to the current grid (tableLayoutPanel) maximum size. This is only relevant if the TLP/grid size changes.

If you add two controls two the same grid location programmatically, the first control in a grid keeps its location. Any subsequently added control gets pushed to the next cell block. If a 'span' is added, it will treat that cell block as used and continue searching for an unused cell block.

Ex: label1, label2 and label3 are added to 0,0.

  • label1 will appear in 0,0
  • label2: 0,1
  • label3: 0,2

Ex 2: label 1 has a row span of 2.

  • label1: 0,0
  • label2: relocated to 0,2
  • label3: 0,3

After you have selected the correct grid point and spans, you can then further optimize your layout using the dock and anchor properties.

Branden
  • 91
  • 1
  • 4