B
Bernie
Sorry, but this ia another whine about VB.Net's lack of
Control Arrays. I am new to VB.Net and I'm building an
application that uses variable number of Label controls
that are created at run time. The number of label
controls will vary between 50 and 200. I have created an
array of labels and placed them on the form. It works
great, BUT I have not been able to raise any events for
these controls. I made a small demo with 9 labels
without using a control array and was able to write an
single event handler for the controls. I am including
code snippets of the code I wrote to generate the array
and the code for the event handler. Any suggestions on
how to create a variable number of controls with events
at run time would be most appreciated.
Bernie
Sub BuildBoard(ByVal N As Integer)
'Create an array of labels, Dot(i)
'N number of dots in a row and the number of rows
Dim i As Integer
Dim X0 As Integer, Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1) ^ 2 - 1) As Label
For i = 0 To (N + 1) ^ 2 - 1
Dot(i) = New Label
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0+35*(i Mod _
(N+1)),Y0+35*(i\(N + 1)))
Dot(i).Size = New Size(5, 5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
Next
End Sub
Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Dot2.Click, Dot3.Click, _
Dot4.Click, Dot5.Click
'Event Handler swaps images in labels Dot2, Dot3, etc.
If DirectCast(sender, Label).ImageIndex = 1 Then
DirectCast(sender, Label).ImageIndex = 0
Else
DirectCast(sender, Label).ImageIndex = 1
End If
End Sub
Control Arrays. I am new to VB.Net and I'm building an
application that uses variable number of Label controls
that are created at run time. The number of label
controls will vary between 50 and 200. I have created an
array of labels and placed them on the form. It works
great, BUT I have not been able to raise any events for
these controls. I made a small demo with 9 labels
without using a control array and was able to write an
single event handler for the controls. I am including
code snippets of the code I wrote to generate the array
and the code for the event handler. Any suggestions on
how to create a variable number of controls with events
at run time would be most appreciated.
Bernie
Sub BuildBoard(ByVal N As Integer)
'Create an array of labels, Dot(i)
'N number of dots in a row and the number of rows
Dim i As Integer
Dim X0 As Integer, Y0 As Integer
X0 = 30
Y0 = 60
Dim Dot((N + 1) ^ 2 - 1) As Label
For i = 0 To (N + 1) ^ 2 - 1
Dot(i) = New Label
Controls.Add(Dot(i))
Dot(i).Location = New Point(X0+35*(i Mod _
(N+1)),Y0+35*(i\(N + 1)))
Dot(i).Size = New Size(5, 5)
Dot(i).ImageList = imgDot
Dot(i).ImageIndex = 0
Next
End Sub
Sub ProcessClick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Dot2.Click, Dot3.Click, _
Dot4.Click, Dot5.Click
'Event Handler swaps images in labels Dot2, Dot3, etc.
If DirectCast(sender, Label).ImageIndex = 1 Then
DirectCast(sender, Label).ImageIndex = 0
Else
DirectCast(sender, Label).ImageIndex = 1
End If
End Sub