Click Event for Member of Label Array

I have a VS 2005 Beta 2 VB program that creates an array of labels at runtime ... several dozen of them. Is there some way to capture click events on these labels so that I can figure out which one was clicked? I am worrried because as I understand it the WithEvents clause cannot be used with arrays but I cannot help but think there isn't some way to do what I want to do. Here is how I created the array:

PrivateSub btnGo_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles btnGo.Click

Dim iAsInteger
Dim jAsInteger
Dim cells(8, 8)As Label
For i = 0To 8
For j = 0To 8
cells(i, j) =
New Label
cells(i, j).Width = 40
cells(i, j).Height = 40
cells(i, j).Text =
CStr(i * 9 + j + 1)
cells(i, j).Top = 150 + i * 42
cells(i, j).Left = 200 + j * 42
cells(i, j).ForeColor = Color.Black
cells(i, j).BackColor = Color.LightBlue
cells(i, j).BorderStyle = BorderStyle.FixedSingle
cells(i, j).TextAlign = ContentAlignment.MiddleCenter
cells(i, j).AutoSize =
False
Me.Controls.Add(cells(i, j))
Next
Next
EndSub


Thanks.

[2985 byte] By [fripper] at [2007-12-16]
# 1

You can use AddHandler to dynamically add an eventhandler. The corresponding method to remove the event handler is RemoveHandler (which I haven't done in this example)



Private
Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim i As Integer
Dim j As Integer
Dim cells(8, 8) As Label
For i = 0 To 8
For j = 0 To 8
cells(i, j) =
New Label
cells(i, j).Width = 40
cells(i, j).Height = 40
cells(i, j).Text =
CStr(i * 9 + j + 1)
cells(i, j).Top = 150 + i * 42
cells(i, j).Left = 200 + j * 42
cells(i, j).ForeColor = Color.Black
cells(i, j).BackColor = Color.LightBlue
cells(i, j).BorderStyle = BorderStyle.FixedSingle
cells(i, j).TextAlign = ContentAlignment.MiddleCenter
cells(i, j).AutoSize =
False

AddHandler cells(i, j).Click, AddressOf Me.LabelClick

Me
.Controls.Add(cells(i, j))
Next
Next
End Sub

Private Sub LabelClick(ByVal sender As Object, ByVal e As EventArgs)
Dim lbl As Label = TryCast(sender, Label)
If lbl IsNot Nothing Then
MessageBox.Show(lbl.Text)
End If
End Sub

Best regards,
Johan Stenberg

JohanStenberg at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Great help, Johan ... you have solved my dilemma. Thank you. I am very slowly beginning to get my arms around VB .Net 2005 ... I wish there was a huge library of sample code for doing things like this. I miss the simply control array feature of VB 6 and have struggled with learning how to do the equivalent in Net.

Thanks again.

fripper at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 3
Just glad to be able to help.

There are resources out there that may be of help, including a couple of free online books. Check out http://msdn.microsoft.com/vbasic/Learning/Migrating/default.aspx#Migrating%20from%20VB6
for some more information.

Best regards,
Johan Stenberg

JohanStenberg at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified