control array with code added controls

D

David Pick

I've a bunch of labels that i haved added to a form using this code
for i = 1 to 12
for j = 1 to 12
dim lbl as new label
me.controls.add(lbl)
with lbl
...
end with
next
next

I would like to have it so that if one of these labels were click it
would change color. Any help would be appreciated. Thanks

- David
 
J

Josip Medved

I would like to have it so that if one of these labels were click it
would change color. Any help would be appreciated. Thanks

First create sub:

Sub SomeClick(sender as object, e as System.EventArgs)
DirectCast(sender, Label).BackColor = Color.Red
End Sub


If your for use AddHandler like this:

for i = 1 to 12
for j = 1 to 12
dim lbl as new label
AddHandler lbl.Click, AddressOf SomeClick
me.controls.add(lbl)
with lbl
...
end with
next
next


This should do it.
 
D

David Pick

Thanks that worked great. Is there any way to figure out which label
was being click?
 
J

Jay B. Harlow

David,
As the example shows: the "sender" parameter is the label that was clicked.

You could assign the label to a locally typed variable

' VS 2005 syntaxDim theLabel As Label = DirectCast(sender, Label)
If theLabel.BackColor = Color.Black Then
theLabel.BackColor = Color.Red
Else
theLabel.BackColor = Color.Black
End If
When you created the label you could use the Label.Tag (inherited from
Control) property to track extra information about the Label. In addition to
Label.Tag I normally use Inheritance to add additional info, via new fields
& properties to the Label control.

Public Class CustomLabel
Inherits Label

Private m_row As Integer
Private m_column As Integer

Public Property Row() As Integer
Get
Return m_row
End Get
Set(ByVal value As Integer)
m_row = value
End Set
End Property

Public Property Column() As Integer
Get
Return m_column
End Get
Set(ByVal value As Integer)
m_column = value
End Set
End Property

End Class
.Row = i
.Column = j

--
Hope this helps
Jay B. Harlow
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


David Pick said:
Thanks that worked great. Is there any way to figure out which label
was being click?
 
D

David Pick

Thanks!
Jay said:
David,
As the example shows: the "sender" parameter is the label that was clicked.

You could assign the label to a locally typed variable

' VS 2005 syntaxDim theLabel As Label = DirectCast(sender, Label)
If theLabel.BackColor = Color.Black Then
theLabel.BackColor = Color.Red
Else
theLabel.BackColor = Color.Black
End If
When you created the label you could use the Label.Tag (inherited from
Control) property to track extra information about the Label. In addition to
Label.Tag I normally use Inheritance to add additional info, via new fields
& properties to the Label control.

Public Class CustomLabel
Inherits Label

Private m_row As Integer
Private m_column As Integer

Public Property Row() As Integer
Get
Return m_row
End Get
Set(ByVal value As Integer)
m_row = value
End Set
End Property

Public Property Column() As Integer
Get
Return m_column
End Get
Set(ByVal value As Integer)
m_column = value
End Set
End Property

End Class
.Row = i
.Column = j
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top