Why don't controls placed in TableLayoutPanels "snap to" their "cells"?

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.

Upvotes: 0

Views: 1925

Answers (1)

Phil Martin
Phil Martin

Reputation: 466

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.

Upvotes: 3

Related Questions