1

How can I set default height of all TableLayoutPanel rows in c# to 16px?

Usually I would do:

for (int i = 0; i < amount_of_rows; i++)
{
    panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 16));
}

But in my case I have 8 columns and unknown amount of rows. Amount on rows is known after all controls are added, not before.

This is my code:

TableLayoutPanel panel = new TableLayoutPanel
{
    BackColor = SystemColors.Control,
    CellBorderStyle = TableLayoutPanelCellBorderStyle.Single,
    AutoScroll = true,
    Width = 500,
    Location = new Point(-1, -1),
    ColumnCount = 8
};
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 16));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 73));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 16));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 73));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 38));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 38));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 52));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 52));

int amountOfRows = 0;
for (...)
{
    // a lot of code which populates panel with generated controls and counts amountOfRows
}
panel.RowCount = amountOfRows;
Controls.Add(panel);

Such code creates rows with 24px height.

Adding ColumnStyles to table after it was populated like this:

for (int i = 0; i < amountOfRows ; i++)
{
    panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 16));
}

works but it makes last row very large.

EDIT: example code including adding a control:

TableLayoutPanel panel = new TableLayoutPanel
{
    BackColor = SystemColors.Control,
    CellBorderStyle = TableLayoutPanelCellBorderStyle.Single,
    AutoScroll = true,
    Width = 500,
    Location = new Point(-1, -1),
    ColumnCount = 8,
    Margin = new Padding(0),
    Padding = new Padding(0)
};
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 16));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 73));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 16));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 73));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 38));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 38));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 52));
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 52));

for (int i = 0; i < 10; i++) // lets assume we don't know it will be added 10x
{
    CheckBox exchangeEnabled = new CheckBox
    {
        Margin = new Padding(0),
        Padding = new Padding(0),
        Dock = DockStyle.Fill,
        CheckAlign = ContentAlignment.MiddleCenter
    };
    panel.Controls.Add(exchangeEnabled, 0, i);
}
Controls.Add(panel);
LukAss741
  • 771
  • 1
  • 7
  • 24

1 Answers1

0

I assume that the reason you get cells with a height of 24 pixels is that you are adding controls with a height of 16 pixels.

Unless you change the values the default Margin and Padding values are 3 pixels for all sides.

This will result in 2x3 + 2x3 extra pixels, adding up to a total height of 24 pixels for the cells.

To control the height you should not only control the height of the controls you add, but also of the TLP's Padding and the child's Margin values.

This is also a way to enlarge rows or columns and the only way I know of aligning/placing controls in a pixel perfect way..

MSDN:

The docking behavior of child controls is the same as other container controls.

The anchoring behavior of child controls in a TableLayoutPanel differs from the behavior in other container controls. If the value of the child control's Anchor property is set to Left or Right, the control will be placed against the left or right border of the cell, at a distance that is the sum of the control's Margin property and the panel's Padding property. If both the Left and Right values are set, the control will be sized to the width of the cell, with the Margin and Padding values taken into account. The behavior for Top and Bottom anchoring is analogous. For more information, see How to: Anchor and Dock Child Controls in a TableLayoutPanel Control.

If you need a child control to mimic the default anchoring behavior in other container controls, you can adjust the Margin and Padding properties to maintain a constant distance between the control's border and the cell's border.

TaW
  • 53,122
  • 8
  • 69
  • 111
  • Thanks but it doesn't change anything. I added Margin = new Padding(0), Padding = new Padding(0) to TableLayoutPanel and its controls. I added an example code including adding a control. Or didn't I completely understand you? – LukAss741 Oct 22 '16 at 09:55
  • Hm, no, you got me right. Can you see the influence when setting a large padding/margin? - What are the max. heights? By default a CheckBox has autosize on, so the height will depend on the Fontsize. – TaW Oct 22 '16 at 10:01
  • Setting Height = 16 on all controls made. Even when margin and padding are 0 it still adds some space around for some reason. After specifying exact height it works. Thanks for pointing me the right direction. – LukAss741 Oct 22 '16 at 11:13
  • At the end I did it by running another loop at the beginning which counts amount of rows and adds 16px rows prior to adding controls. I had to do it like that because it is not possible to change textbox size which is single line. It is only possible to set multiline textbox size which I don't want. Winforms is apparently not ad advanced as css ;). – LukAss741 Oct 22 '16 at 11:32