Help on Event Handling during Run Time

Changedsoul

New member
Joined
Oct 25, 2006
Messages
3
Programming Experience
Beginner
Hi, I have trid to look through MSDN and cant seem to find an answer. Hopefully someone here can help, and I know its an easy fix...at least I think it is, lol.

I created 42 Label controls during runtime in an array with this:
Dim label(42) As Label

I then ran it thru a For loop to create them and set properties:
For i = 1 To 42
label(i) =
New Label
Panel1.Controls.Add(label(i))
label(i).AutoSize =
False
label(i).TextAlign = ContentAlignment.MiddleCenter
label(i).Width = Width
label(i).Height = Height
Next

My problem now is that I want to add a Mouse over event and some other events to these lables after they are created. How can I do this, and could you please give a quick easy to see solution.

Thanks so Much.
 
I'm going to steer you in a different direction with this. Look into Generics at MSDN much better than an array and easier to manipulate.

VB.NET:
'Declared At The Class Level
 
Private MyGenericListOfLabel As New List(Of Label)
 
Private Sub CreateLabelList()
 
Dim MyLabel as Label
 
For I as Integer = 0 to 41
 
'Create a New Label
 
MyLabel = New Label
 
'Add Code Here For Label Size And Location
 
'Add The Event Handler
 
AddHandler MyLabel.Click, AddressOf MyLabelClick
 
'Add The Label To The ControlCollection
 
Me.Controls.Add(Label)
 
'Add The Label To The List
 
MyGenericListOfLabel.Add(Label)
 
Next
 
End Sub

Then create a sub in the class that has the same delegate signature and the event to want to wire your label to,

VB.NET:
Private Sub MyLabelClick(Byval Sender as Object , Byval e As Eventargs)
 
'Label can be accessed here with the sender parameter.
 
End Sub

Just knocked this up without testing it, becuase i'm not at my dev PC so any errors and get back to me. But it will give you the general idea.
 
I am not familiar with Generics. I am pretty new to programming in general. I will check it out and see what I can learn. Thanks for the Help.
 
Back
Top