views:

41

answers:

1

I'm trying to build a TableLayoutPanel on a WinForm and want it to behave exactly like a plain old HTML table.

One requirement is that this table needs to be built programmatically. This is what I have so far:

foreach (var RowLinq in ResultLinq)
{
    RichTextBox RT = new RichTextBox();
    RT.BorderStyle = BorderStyle.None;
    RT.Text = RowLinq.Result.ResultName;
    RT.Dock = DockStyle.Fill;

    TableLayoutPanel.RowCount++;
    TableLayoutPanel.RowStyles.Add(new RowStyle(System.Windows.Forms.SizeType.AutoSize));
    TableLayoutPanel.Controls.Add(rt1, 0, tableLayoutPanel5.RowCount - 1);
}

So this builds a row for each row in my Linq result. This works pretty well except for one thing: the height doesn't adjust at all and is completely fixed. I need the height to grow and shrink depending on the height of the text inside each cell.

I need your help with this one big time, thanks Stack-o

A: 

Set its AutoSize property to True.

Hans Passant
The TableLayoutPanel? This doesn't affect the height unfortunately. Also, I'm trying to set RT.Height to autosize, but I don't know how to do that. I've tried RT.Height = SizeType.AutoSize, but I guess that's the wrong syntax...
Soo
Works on my machine. You can try it out in the designer. Put a button in a cell and set its Dock property to Fill. Resize the button and watch the TLP grow.
Hans Passant
See that's the strange part, it works when you build it in the designer, but when you build programmatically, things start to get all wonky. If you can get the height to adjust using the code in the OP + some new code, I'd greatly appreciate it, thanks!
Soo