Link to home
Start Free TrialLog in
Avatar of pvg1975
pvg1975Flag for Argentina

asked on

Working with Labels inside TableLayoutPanel

Hello all,

I have a TableLayoutPanel I am populating with 450 Labels:

        For t = 0 To 29
            For x = 0 To 14
                Counter = Counter + 1
                Dim tBox As New Label With {.Name = "Table" & Counter, .Text = Counter, .BackColor = Color.Green, .ForeColor = Color.White, .Height = 16, .Width = 45, .TextAlign = ContentAlignment.MiddleCenter}
                TABLA.Controls.Add(tBox, x, t)
            Next
        Next

Each label has a name as Table1, Table2, Table3, etc

Here are my questions:

1) After the TableLayoutPanel is populated, how do I change the background color of a Label? As you can see above, all labels are background colored as  .BackColor = Color.Green I need to change some of them to RED

2) How to I capture a CLICK event on one of the Labels?

As you can see, each label has a number (from 1 to 450), I just simply need to display the text contained on the clicked label into another label called XTABLE

Thanks!
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

You can search for it by name like this:
Dim i As Integer = 3

Dim ctls() As Control
ctls = Me.Controls.Find("Table" & i, True)
If ctls.Length > 0 AndAlso TypeOf ctls(0) Is Label Then
    Dim lbl As Label = CType(ctls(0), Label)
    lbl.BackColor = Color.Red
End If

Open in new window

Avatar of pvg1975

ASKER

Thanks idle_Mind. Where should I place that code? On the TableLayoutPanel?

Im new at this :)
Avatar of pvg1975

ASKER

Oh I get it, but is it possible to click on the labels?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pvg1975

ASKER

Thanks!!