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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:33:05+00:00 2026-06-03T14:33:05+00:00

I am dynamically creating a TableLayoutPanel, and then dynamically creating Labels and TextBoxes to

  • 0

I am dynamically creating a TableLayoutPanel, and then dynamically creating Labels and TextBoxes to put inside it.

It would seem logical that I could just assign the number of columns and rows to the TableLayoutPanel:

tableLayoutPanelGreatGooglyMoogly = new TableLayoutPanel();
tableLayoutPanelGreatGooglyMoogly.RowCount = NUMBER_OF_ROWS;
tableLayoutPanelGreatGooglyMoogly.ColumnCount = NUMBER_OF_COLUMNS;

…create the controls to place inside it:

Label lbl = new Label();
lbl.Parent = tableLayoutPanelGreatGooglyMoogly;
. . .

…and then put the created control[s] in the specified “cell” (column and row):

tableLayoutPanelGreatGooglyMoogly.SetColumn(lbl, ACol); // "ACol" is the current column
tableLayoutPanelGreatGooglyMoogly.SetRow(lbl, i); // "i" is the current row

…but that’s not working – neither if I specify the width and height values for the dynamically created child controls or if I don’t (in which case they are too large – specifically, their width is too great).

UPDATE

I added this code, and it makes no difference:

// ROWS

tableLayoutPanelGreatGooglyMoogly.RowCount = NUMBER_OF_ROWS;
TableLayoutRowStyleCollection rowStyles =
    this.tableLayoutPanelGreatGooglyMoogly.RowStyles;

    foreach (RowStyle rStyle in rowStyles) {
        rStyle.SizeType = SizeType.Percent;
        rStyle.Height = 8;
    }

// COLUMNS
tableLayoutPanelGreatGooglyMoogly.ColumnCount = TOTAL_NUMBER_OF_COLUMNS;
TableLayoutColumnStyleCollection columnStyles =
    this.tableLayoutPanelGreatGooglyMoogly.ColumnStyles;

    foreach (ColumnStyle cStyle in columnStyles) {
        cStyle.SizeType = SizeType.Percent;
        cStyle.Width = 12;
    }

UPDATE to the UPDATE

I see that at design-time, a Label or TextBox (presumably, any control) has a Cell[col,row] property. I would like to access that dynamically, if it is not readonly, so that I could set:

lbl.Cell = i,i
txtbox.Cell = i+1,i

Is that possible to do in code? The “Cell” property does not seem to be recognized (understandably, I guess) at that time.

UPDATE to the UPDATE REVISITED

I added this line of code:

tableLayoutPanelGreatGooglyMoogly.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;

…and now I see that the labels and textBoxes actually are in the cells (columns and rows) I’m expecting them to inhabit.

However, I need to get the labels to move down from the upper left corner of their cells to the center of the cell (both vertically and horizontally).

Even at design time (with a “test”/temporary TableLayoutPanel on the form), added Labels do not respond to changes to the TextAlign property when they are sitting inside a TableLayoutPanel cell – no matter what I set TextAlign to (“Middle Center” seems the most sensible), they stubbornly remain affixed to the top left of the cell.

Similary, changing the Labels’ Location.X and Location.Y at design time does nothing. The Labels stick to the NW corner of the cell like a barnacle to a long-lost anchor.

  • 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-03T14:33:07+00:00Added an answer on June 3, 2026 at 2:33 pm

    An important part of using the GridLayoutPanel that is rarely mentioned is the use of the Anchor property in child controls.

    The Anchor property determines which edge of the cell each child control will extend to.

    When you create your labels you do it like this:

    Label lbl = new Label();
    lbl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
    lbl.Parent = tableLayoutPanelGreatGooglyMoogly;
    

    It should stretch the labels to the full size of the cell.

    See http://msdn.microsoft.com/en-us/library/w4yc3e8c(v=vs.80).aspx in the “Positioning Controls Within Cells Using Docking and Anchoring” secton.

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

Sidebar

Related Questions

I just found out that in FF, if you are dynamically creating an OPTION
So if I'm dynamically creating textviews in a foreach loop and want to assign
I am dynamically creating divs within a jquery accordion that are loaded with data
I'm dynamically creating option elements for a drop down menu using Javascript and would
I am dynamically creating a table that contains a textbox in each cell. The
I am dynamically creating an iframe with javascript inside the body. iframe = m_oYDOM.get(ifAdwords);
I am dynamically creating textboxes using document.createElement('input') and adding a break between them using
I'm dynamically creating some <div> elements and then filling their innerHTML properties with text.
I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good.
Having issues with dynamically creating <select> tags and then populating them with XML values.

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.