Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Working with Labels inside TableLayoutPanel

Posted on 2011-09-13
5
Medium Priority
?
410 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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