Solved

Working with Labels inside TableLayoutPanel

Posted on 2011-09-13
5
405 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 86

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 86

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

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 …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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