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 6473787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:30:30+00:00 2026-05-25T06:30:30+00:00

I may not be using the right control for what I want. I’m filling

  • 0

I may not be using the right control for what I want. I’m filling a table with controls and I want each column to automatically size to the controls contained within it. For example, a column of textboxes will be wider than a column of checkboxes. I don’t want to fiddle with measuring if I can help it, due to the complexities of different OS, different DPI, different fonts, etc. The table can expand horizontally to fit the controls, with a scrollbar. How is this possible with a TableLayoutPanel – or some other control?

Thanks.

Edited to add code:

    private void UpdateLocations()
    {
        tableLayoutPanel1.RowCount = CurrentSchedule.location.Length + 1;
        tableLayoutPanel1.ColumnCount = 7;
        int row = 1;
        int timeWidth = TextRenderer.MeasureText("00:00:00x", tableLayoutPanel1.Font).Width;

        Label lab = new Label();
        lab.Text = "Location";
        tableLayoutPanel1.Controls.Add(lab, 0, 0);

        lab = new Label();
        lab.Text = "Arrive";
        tableLayoutPanel1.Controls.Add(lab, 1, 0);

        lab = new Label();
        lab.Text = "Depart";
        tableLayoutPanel1.Controls.Add(lab, 2, 0);

        lab = new Label();
        lab.Text = "Pass?";
        tableLayoutPanel1.Controls.Add(lab, 3, 0);

        lab = new Label();
        lab.Text = "Path";
        tableLayoutPanel1.Controls.Add(lab, 4, 0);

        lab = new Label();
        lab.Text = "Plat";
        tableLayoutPanel1.Controls.Add(lab, 5, 0);

        lab = new Label();
        lab.Text = "Line";
        tableLayoutPanel1.Controls.Add(lab, 6, 0);

        foreach (location loc in CurrentSchedule.location)
        {
            TextBox tb = new TextBox();
            tb.Text = loc.locationID;
            tableLayoutPanel1.Controls.Add(tb, 0, row);

            tb = new TextBox();
            tb.Text = loc.arrivalTime;
            tb.Width = timeWidth;
            tableLayoutPanel1.Controls.Add(tb, 1, row);

            tb = new TextBox();
            tb.Text = loc.departureTime;
            tb.Width = timeWidth;
            tableLayoutPanel1.Controls.Add(tb, 2, row);

            CheckBox cb = new CheckBox();
            cb.Checked = loc.passingTime;
            tableLayoutPanel1.Controls.Add(cb, 3, row);

            tb = new TextBox();
            tb.Text = loc.pathCode;
            tableLayoutPanel1.Controls.Add(tb, 4, row);

            tb = new TextBox();
            tb.Text = loc.platformCode;
            tableLayoutPanel1.Controls.Add(tb, 5, row);

            tb = new TextBox();
            tb.Text = loc.lineCode;
            tableLayoutPanel1.Controls.Add(tb, 6, row);

            row++;
        }
        /*for (int idx = 0; idx < tableLayoutPanel1.RowCount; idx++)
        {
            tableLayoutPanel1.RowStyles[idx].SizeType = SizeType.AutoSize;
        }
        for (int idx = 0; idx < tableLayoutPanel1.ColumnCount; idx++)
        {
            tableLayoutPanel1.ColumnStyles[idx].SizeType = SizeType.AutoSize;
        }*/
    }

(Yes it needs heavy refactoring – I’m just trying to get it to work first)

The commented out bits cause out of bounds exceptions, even though logically (to me) it shouldn’t. The range appears limited to whatever I set at design time, not at runtime.

  • 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-05-25T06:30:31+00:00Added an answer on May 25, 2026 at 6:30 am

    Sorry, but what’s wrong with having the columns set to Autosize? That’s what TableLayoutPanel does, size columns to the fit the controls within it. Expanding the table and having a scrollbar would require you to set the tables Autosize property to true, then sit the TableLayoutPanel within another panel that has scrollbars enabled. But the column sizing should work out of the box if unless I’m misunderstanding your requirements.

    Just to make sure, you are going to the columns property and setting each column’s SizeType to AutoSize right? Not just the AutoSize property of the table itself?

    enter image description here

    is this what you want?

    -Post code:

    Thanks for the code. I’d suggest that you use designer to do a lot of this. At least to set up the columns, set them to autosize, and add the heading labels.

    You also might want to check out the Datagrid control and bind that to your location list.

    To get this method working though:

    1) the reason your columns look the same size is because the heading labels you’re using aren’t autosizing. They’re all x pixels wide and that’s stretching the columns. Do this:

    Label lab = new Label();
    lab.AutoSize = true;
    lab.Text = "Location";
    tableLayoutPanel1.Controls.Add(lab, 0, 0);
    

    You’ll also need to set the AutoSize property to true on the CheckBox control and any other labels you add as content.

    2) Setting the RowCount and ColumnCount won’t affect the RowStyles or ColumnStyles collection. You’ve got 7 Columns but only 2 ColumnStyles. Try:

    tableLayoutPanel1.ColumnStyles.Clear();
    for (int i = 0; i < tableLayoutPanel1.ColumnCount; i++)
    {
       tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
    }
    
    tableLayoutPanel1.RowStyles.Clear();
    for (int i = 0; i < tableLayoutPanel1.RowCount; i++)
    {
       tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize));
    }
    

    Only other thing to look out for is that some controls will be misaligned in the rows (labels appear too high for example). To fix that set the Margin property, normally to 3,6,3,0 to align them with textboxes and checkboxes etc.

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

Sidebar

Related Questions

I'm using a huge binary tree like structure whose nodes may or may not
I am using Visual C# Winforms to control a WebBrowser object. Specifically I want
In Android, I want to create a particular control which may require to set
This may not be a programming question, but I don't know where to ask
This may not be the kind of question one should ask on StackOverflow, but
It may not be so obvious how respond_to? works in ruby. Consider that: class
This may not be possible for the carousel but is there a way to
I may not understand what the return statement does(I thought it just returned a
I fear this just may not be possible, but I'm trying to create a
The title may not seem very clear - was not sure how to explain

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.