Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8402379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:02:35+00:00 2026-06-09T22:02:35+00:00

I am dynamically adding controls to a TableLayoutPanel. It has 12 rows and 32

  • 0

I am dynamically adding controls to a TableLayoutPanel.

It has 12 rows and 32 columns.

I’ve divided the widths of the columns so that those with labels are twice as wide
as those with TextBoxes, and the percentages add up to 100:

labels = 5%
textBoxes = 2.5%

As there are 24 textboxes per row and 8 labels per row, this equals 100% (40% labels, 60% textBoxes).

HOWEVER, the last column is a spacehog, as can be seen here:

http://warbler.posterous.com/the-32-column-is-wider-than-the-rest

And I need that space for the labels to show their entire selves: they are really five characters wide (“00:00” … “23:45”) not four, as displayed.

Based on empirical evidence (previous trial-and-error) I could reduce the width of the TLP to skinny up that last column, but that doesn’t solve my label-cells-too-skinny problem.

This is how I’m slapping the controls on the TLP (I’ve set the column width percentages at design-time, via the properties page for Columns collection)

private void AddPlatypusScheduleControlsToTableLayoutPanel()
{
    try
    {
        this.SuspendLayout();
        tableLayoutPanelPlatypusSchedule.ColumnCount = PLATYPUS_SCHEDULE_COL_COUNT;
        int ColNum = 0;
        int RowNum = 0;
        int LoopCounter = 1;
        var dt = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);

        while (LoopCounter <= QUARTER_HOUR_COUNT)
        {
            string lblName = string.Format("labelPS{0}", LoopCounter);
            var lbl = new Label
                {
                    Name = lblName,
                    Dock = DockStyle.Fill,
                    Margin = new Padding(),
                    Font = new Font("Microsoft Sans Serif", 7),
                    TextAlign = ContentAlignment.MiddleCenter,
                    Text = dt.ToString("HH:mm")
                };
            tableLayoutPanelPlatypusSchedule.Controls.Add(lbl, ColNum, RowNum);

            string txtbxName1 = string.Format("textBoxLSA{0}", LoopCounter);
            var txtbxa = new TextBox { Name = txtbxName1, Dock = DockStyle.Fill, Margin = new Padding() };
            string txtbxName2 = string.Format("textBoxLSB{0}", LoopCounter);
            var txtbxb = new TextBox { Name = txtbxName2, Dock = DockStyle.Fill, Margin = new Padding() };
            string txtbxName3 = string.Format("textBoxLSC{0}", LoopCounter);
            var txtbxc = new TextBox { Name = txtbxName3, Dock = DockStyle.Fill, Margin = new Padding() };
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxa, ColNum + 1, RowNum);
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxb, ColNum + 2, RowNum);
            tableLayoutPanelPlatypusSchedule.Controls.Add(txtbxc, ColNum + 3, RowNum);

            dt = dt.AddMinutes(15);
            RowNum++;
            LoopCounter++;
            if (RowNum == 12)
            {
                ColNum = ColNum + 4;
                RowNum = 0;
            }
        }
    }
    finally {
        this.ResumeLayout();
    }
}

UPDATE

I thought maybe the problem was the width of the TLP was not exactly divisible by 32 (it was 619); so I widened it to 640, yet the same problem remains.

UPDATE 2

To get it to work, I had to use absolute sizes for the columns rather than percentages*, and increase the width of the label-holding columns to 35 (which leaves only 15 for the textBox-holding columns at a TLP width of 640).

  • WPF can be a bear at times (more like a Kodiak than Winnie), but it sure fills the bill for situations like this, where I worry about absolute sizes going all to Dallas on different resolutions/screen sizes, etc.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:02:36+00:00Added an answer on June 9, 2026 at 10:02 pm

    So maybe this is due to floating point math and integral widths.

    The ClientSize.Width of a control is an integer. The table layout, regardless if you specify a floating point value, will ultimately need to convert that to something integral.

    I threw together an example similar to yours. When I ran the example, I grabbed the control’s ClientWidth value, which was 284 for my window’s size.

    The cell borders are going to be some width and as a guess I said they were 1. So, let’s assume 284 minus the 33 for the borders, so that is 251 left over that needs to be split up into 32 columns.

    For those narrow columns, the value was 0.025:

    0.025 * 251 = 6.275, truncated to 6
    

    So there’s an extra 0.275 for each narrow column.

    For those wider columns, the value was 0.05:

    0.05 * 251 = 12.55, truncated to 12
    

    So there’s an extra 0.55 for each wide column.

    24 * 6 = 144 width for the 24 narrow columns

    8 * 12 = 96 width for the 8 wide columns

    Total of 240.

    11 left over, so the extra goes to the last column and it ends up being what one would think it would be (6) + the extra (11) and it ends up being, say, 3x as wide as you would expect, due to floating point math.

    Anyhow, that’s my guess on why it looks that way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am adding labels dynamically to a page, there are a few labels that
I am adding 2 controls Checkbox1 and Textbox1 dynamically. Textbox1 has been disabled by
I'm attempting to dynamically add both columns and rows to a TableLayoutPanel. I have
I´m adding controls dynamically onto my form, but I can't see those new controls.
I’m adding web user controls to a page dynamically. Using the LoadControl method that
I'm adding HTML controls dynamically using java script. For instance, I have one button
I am adding a user control dynamically on a page, the user control has
I am dynamically adding columns to a table by using document.createElement(th) var newTH =
I am dynamically adding labels to a form and I would like to set
I am dynamically adding and replacing controls in a winform panel at runtime Even

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.