Solved

Working with Labels inside TableLayoutPanel

Posted on 2011-09-13
5
393 Views
Last Modified: 2012-05-12
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!
0
Comment
Question by:pvg1975
  • 3
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36531044
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

0
 

Author Comment

by:pvg1975
ID: 36531091
Thanks idle_Mind. Where should I place that code? On the TableLayoutPanel?

Im new at this :)
0
 

Author Comment

by:pvg1975
ID: 36531100
Oh I get it, but is it possible to click on the labels?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36531141
Sure...use AddHandler() when you create them to "wire up" an event handler:
*Use the "sender" parameter to get a reference to the source Label in the event*
Dim tBox As New Label ...
AddHandler tBox.Click, AddressOf tBox_Click

...

    Private Sub tBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim lbl As Label = CType(sender, Label)
        ' ... use "lbl" somehow ...
    End Sub

Open in new window

0
 

Author Closing Comment

by:pvg1975
ID: 36532113
Thanks!!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now