(As a warning - if at any point you think "umm why don't you just use X to get your 2 column display - I'm all for that! This is the only way I came up with).

I want to end up nice looking display of data on the form like this:
Test 1 Result 1
Test 2 Result 2
Test 3 Result 3
Test 4 Result 4
Test 5 Result 5

And have the option to color the results green (for "pass") and red (for "fail").


If I make a TableLayoutPanel and add 10 labels to it like this:

For i As Integer = 1 To 10 Step 2
            'setup first column

            'create a new label control, name it, and populate it with the test name
            Dim myLabelName As New Label
            myLabelName.Name = "lblTest_" + i.ToString + "_name"
            myLabelName.Text = "Test " + i.ToString
            Me.TableLayoutPanel1.Controls.Add(myLabelName)

            'setup second column

            'create a new label control, name it, and populate it with the test name
            'this will automatically be in the second column because the tableLayoutPanel has two columns
            'and items are added like this by default:
            '1 2
            '3 4
            '5 6
            Dim myLabelResult As New Label
            myLabelResult.Name = "lblTest_" + (i + 1).ToString + "_result"
            myLabelResult.Text = "Test " + (i + 1).ToString

            Me.TableLayoutPanel1.Controls.Add(myLabelResult)
        Next

there is a very awkwardly big space between some of the controls that seems to depend on how big the TableLayoutPanel is.

The TableLayoutPanel does the same thing with all combinations of autosize and autosize mode. It also happens with autoscroll either on or off, because there is not more content than will fit in the table.

Does anyone know to make these labels spaced vertically evenly? (Or, again, a better way to display this type of data to the user?)

Thanks!

Dave

Tried this? You can set it in the properties too.
Note: Docking should be None or else the shrink won't work.

Me.TableLayoutPanel1.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink

However I would suggest using a ListView control or a DataGridView with 2 columns for such data display.

Hm I tried setting AutoSizeMode to GrowAndShrink and docking was indeed off, but still getting a really goofy result:
http://rpi.edu/~doriad/TableLayoutPanel.zip

I'd be all for a List/DataGridView. I've used them in the past for displaying database tables, but I always thought it was really hard to use them to display data generated by the application (not in a database). If I am wrong, can someone point me to an example or briefly explain how to produce a few values at run time and display them in one of these types of controls?

Thanks,

Dave

This article has been dead for over six months. Start a new discussion instead.