Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / March 2007

Tip: Looking for answers? Try searching our database.

TableLayoutPanel - equal percentage sizes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Paul E Collins - 22 Mar 2007 16:37 GMT
I am using TableLayoutPanel for a layout with a varying number of rows
and columns.

In the most common case, I want one row with five columns, and I want
the horizontal space divided equally between the columns (i.e. each
one should be 20% of the total width).

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?

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 - 22 Mar 2007 17:36 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.