0
votes

I have a TableLayoutPanel, ug_degrees, with 3 columns and 1 row. Each cell gets dynamically populated with another TableLayoutPanel, degreePanel, containing 1 label and 1 textbox.

I need to get my layout to look something like this:

enter image description here

Right now my layout looks like this:

enter image description here

I'm at a loss for why I have those giant gaps between the cells, and why the row will not expand to fill its contents (a label & a textbox). I have tried to set the whole TableLayoutPanel's autosize property to true, but the columns get resized, even if I set only the rows' sizetype to autosize as well.

The properties behind the table shown are default. All non-default properties are customized in C# below.

// Dynamically load undergraduate degrees
int row = 0;
for (int i = 0; i < degrees.undergraduate.Count; i++) {
    // Create and populate panel for each degree
    TableLayoutPanel degreePanel = new TableLayoutPanel();
    degreePanel.ColumnCount = 1;
    degreePanel.RowCount = 2;
    degreePanel.AutoSize = true;
    foreach (RowStyle style in degreePanel.RowStyles) {
        style.SizeType = SizeType.AutoSize;
    }
    degreePanel.BorderStyle = BorderStyle.FixedSingle;
    //degreePanel.Margin = new Padding(0);

    Label degTitle = new Label();
    degTitle.Text = degrees.undergraduate[i].title;
    degTitle.Dock = DockStyle.Fill;

    TextBox degDesc = new TextBox();
    degDesc.ReadOnly = true;
    degDesc.Multiline = true;
    degDesc.Dock = DockStyle.Fill;
    degDesc.Text = degrees.undergraduate[i].description;
    SizeF size = degDesc.CreateGraphics()
                .MeasureString(degDesc.Text,
                                degDesc.Font,
                                degDesc.Width,
                                new StringFormat(0));
    degDesc.Height = (int)size.Height;

    degreePanel.Controls.Add(degTitle, 0, 0);
    degreePanel.Controls.Add(degDesc, 0, 1);
    ug_degrees.Controls.Add(degreePanel, i, row);

    // Resize rows and columns (only after adding controls)
    foreach (RowStyle style in ug_degrees.RowStyles) {
        style.SizeType = SizeType.AutoSize;
    }

    // Jump to next row if current row is full
    if ((i+1) % 3 == 0) {
        row++;
    }
1
Can you show your code that creates the ug_degrees table?Tim
I used Designer to create ug_degrees, and it has all the default values. It also has 3 columns and 1 row.aucamort
Ah, ok. I'm not familiar enough with the winforms designer without testing it, but I would suggest checking all values there as well.Tim
I'm hoping to set any values in C# since the table is populated dynamicallyaucamort
You can try setting values on ug_degrees after it's created. My point is, regardless of when/how it's created, a lot of times UI issues come down to setting a value on the container, instead of on the individual item.Tim

1 Answers

1
votes

Add degreePanel.Dock = DockStyle.Fill