tj_ush
tj_ush

Reputation: 69

Outputting 2D array to form using TableLayout Panel

I am trying to output a 2D array to a form using the tablelayout panel, however when I run the code I have written to do this, the form shows nothing (although no errors are actually thrown up when running it), so I am unsure as to why this isn't working?

Here's my code:

Sub OutputUsingTableLayoutPanel(ByRef arrtruthtable(,) As String)
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim l As Integer
    Dim TableRowsToAdd As Integer = itruthtablerows - 1
    Dim TableColumnsToAdd As Integer = itruthtablecolumns - 1
    Dim NewLabel As New Label()

    TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.AutoSize))
    TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))

    Do Until i = TableColumnsToAdd
        TableLayoutPanel1.ColumnCount += 1
        i += 1
    Loop

    Do Until j = TableRowsToAdd
        TableLayoutPanel1.RowCount += 1
        j += 1
    Loop

    Do Until k = TableLayoutPanel1.ColumnCount
        Do Until l = TableLayoutPanel1.RowCount
            TableLayoutPanel1.Controls.Add(NewLabel, k, l)
            NewLabel.Name = "lbl" & k & l
            NewLabel.Text = arrtruthtable(k, l)
            l += 1
        Loop
        k += 1
        l = 0
    Loop

End Sub

Any help or ideas as to why this isn't working would be much appreciated!

Upvotes: 0

Views: 636

Answers (1)

tinstaafl
tinstaafl

Reputation: 6948

There are several things to consider in your code:

ColumnCount and RowCount are integer properties that you can manipulate directly.

You should declare NewLabel inside the inner loop so that each one that gets added is a new object.

You should set the properties of NewLabel before you add it to the tablelayoutpanel

For loops, for this type of iteration, are much more intuitive and easier to read.

2 dimensional arrays index by row,column but the tablelayoutpanel uses column,row

When you need to access an array that's being passed into your sub routine you can get the length of the individual dimensions rather than relying on a global variable.

here's some revised code to look at:

    Dim test(,) As String = {{"A", "A1"}, {"B", "B1"}, {"C", "C1"}}
    OutputUsingTableLayoutPanel(test)

Sub OutputUsingTableLayoutPanel(ByRef arrtruthtable(,) As String)

    Dim TableRowsToAdd As Integer = arrtruthtable.GetLength(0)
    Dim TableColumnsToAdd As Integer = arrtruthtable.GetLength(1)

    TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.AutoSize))
    TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
    TableLayoutPanel1.ColumnCount = TableColumnsToAdd
    TableLayoutPanel1.RowCount = TableRowsToAdd

    For k = 0 To TableColumnsToAdd - 1
        For l = 0 To TableRowsToAdd - 1
            Dim NewLabel As New Label()
            NewLabel.Name = "lbl" & k & l
            NewLabel.Text = arrtruthtable(l, k)
            TableLayoutPanel1.Controls.Add(NewLabel, k, l)
        Next
    Next

End Sub

Here's a simple way to get the formatting even:

    TableLayoutPanel1.ColumnCount = TableColumnsToAdd
    TableLayoutPanel1.RowCount = TableRowsToAdd
    TableLayoutPanel1.RowStyles.Clear()
    For I = 1 To TableRowsToAdd
        TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Percent, 100 \ TableRowsToAdd))
    Next
    TableLayoutPanel1.ColumnStyles.Clear()
    For I = 1 To TableColumnsToAdd
        TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100 \ TableColumnsToAdd))
    Next

Upvotes: 1

Related Questions