Programmatically setting controls visibility

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
I have a panel with 5 labels and 5 listviews in it. By default they are hidden. At runtime I want only the ones shown that are needed. For example, if there are enough players for three tables then three tables should show up. I know how to make the controls visible, but not sure how to control how many are actually displayed. I also think I may have to do seperate loops and seperate the control types?

VB.NET:
Private Sub Set_Tables()
        Dim tables As Long

        tables = Math.Ceiling(NumPlayers / NumPerTable)

        For x = 1 To tables
            For Each ctrl As Control In panelTables.Controls
                Dim lv As ListView = CType(ctrl, ListView)
                lv.Visible = True
            Next
        Next

    End Sub

I could do it with a big select statment, but it seems kind of messy

VB.NET:
Select Case Math.Ceiling(NumPlayers / NumPerTable)
            Case 1
                lblTbl1.Visible = True
                listViewTbl1.Visible = True
            Case 2
                lblTbl1.Visible = True
                listViewTbl1.Visible = True
                lblTbl2.Visible = True
                listViewTbl2.Visible = True
            Case 3
                lblTbl1.Visible = True
                listViewTbl1.Visible = True
                lblTbl2.Visible = True
                listViewTbl2.Visible = True
                lblTbl3.Visible = True
                listViewTbl3.Visible = True
            Case 4
                lblTbl1.Visible = True
                listViewTbl1.Visible = True
                lblTbl2.Visible = True
                listViewTbl2.Visible = True
                lblTbl3.Visible = True
                listViewTbl3.Visible = True
                lblTbl4.Visible = True
                listViewTbl4.Visible = True
            Case 5
                lblTbl1.Visible = True
                listViewTbl1.Visible = True
                lblTbl2.Visible = True
                listViewTbl2.Visible = True
                lblTbl3.Visible = True
                listViewTbl3.Visible = True
                lblTbl4.Visible = True
                listViewTbl4.Visible = True
                lblTbl5.Visible = True
                listViewTbl5.Visible = True
            Case Else
        End Select
 
Last edited:
Why do you need a Long to store the number of tables if the maximum is 5?
VB.NET:
Dim tableCount As Integer = CInt(Math.Ceiling(playerCount / playersPerTable))

Me.ListView5.Visible = (tableCount > 4)
Me.ListView4.Visible = (tableCount > 3)
Me.ListView3.Visible = (tableCount > 2)
Me.ListView2.Visible = (tableCount > 1)
Me.ListView1.Visible = True
 
Back
Top