Hello Paul,
>I think the code that follows should achieve what I just said, but it
>doesn't. Rather, the four right-hand buttons take up only as much space as
>they need, and the leftmost one gets all of the remaining space. What's
>wrong here, and how should I do it instead?
It looks like your table control exists already when that code is executed
- have you created it at design time? In that case, the panel will always
have column definitions already, and there is no way at design time to
delete all columns, you will always keep at least one. So when you're
adding additional ColumnStyles in that loop in your code, you end up with
at least 6 columns instead of the five you expect, which is probably the
reason for your problem. Try debugging through your code and checking out
the value for table.ColumnStyles.Count to see what's going on.
I tried using the following code, which works just fine - in this case,
there are actually no column styles at all before the loop and the result
is as expected.
TableLayoutPanel table = new TableLayoutPanel( );
this.Controls.Add(table);
table.Dock = DockStyle.Fill;
table.Controls.Clear( );
table.ColumnCount = 5;
table.RowCount = 1;
for (int i = 0; i < 5; i++) {
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 20));
table.Controls.Add(new Button( )); // for example
}
Oliver Sturm

Signature
http://www.sturmnet.org/blog
Paul E Collins - 22 Mar 2007 17:52 GMT
> It looks like your table control exists already when that code
> is executed - have you created it at design time? In that case,
> the panel will always have column definitions already
You're right. Thanks. I fixed it by adding a line:
innerTable.Controls.Clear();
innerTable.ColumnStyles.Clear(); // <-- along with the columns
themselves
Eq.