Solved

Working with Labels inside TableLayoutPanel

Posted on 2011-09-13
5
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…

735 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