Creating control arrays at runtime


O

olds350racer

I am a newbie but have come across a question I hope someone can help
with.
Given the following code for a UserControl:

' Declares in the class
Private intCount as Interger
Private myLabel() as Label
' Code in the Load event of my UserControl
Dim xCount as Integer
intCount = 10
redim myLabel(intCount)
For xCount = 0 to 10
myLabel(xCount) = New Label
Me.Controls.Add(myLabel(xCount))
Next

How would I respond to an event (ie-Click) of one of my myLabel
controls? Given that the control does not exist during the design
phase, there is no event to respond to. I know this sounds like a
newbie question, but I would appreciate if anyone could point me in
the right direction.
 
Ad

Advertisements

L

lord.zoltar

I am a newbie but have come across a question I hope someone can help
with.
Given the following code for a UserControl:

' Declares in the class
Private intCount as Interger
Private myLabel() as Label
' Code in the Load event of my UserControl
Dim xCount as Integer
intCount = 10
redim myLabel(intCount)
For xCount = 0 to 10
myLabel(xCount) = New Label
Me.Controls.Add(myLabel(xCount))
Next

How would I respond to an event (ie-Click) of one of my myLabel
controls? Given that the control does not exist during the design
phase, there is no event to respond to. I know this sounds like a
newbie question, but I would appreciate if anyone could point me in
the right direction.
I'm a bit confused, you're adding labels to an array?
Probably best to do that in the Load event.
 
O

olds350racer

I am. I'm adding an array of labels during the Load event of the
UserControl.
 
J

jayeldee

How would I respond to an event (ie-Click) of one of my myLabel
controls? Given that the control does not exist during the design
phase, there is no event to respond to. I know this sounds like a
newbie question, but I would appreciate if anyone could point me in
the right direction.
You'd want a sub to handle the Click( ) event and you'd add a handler
for the event as you iterate through adding the controls.

Private Sub OhGodSomeoneClickMyLabelPlease(ByVal sender As Object,
ByVal e As System.EventArgs)
' Do stuff here
End Sub

Then you could use AddHandler in the loop
 
L

lord.zoltar

I am. I'm adding an array of labels during the Load event of the
UserControl.
Oh sorry, misread the question. so you want to be able to respond to
mylabel(1) click, mylabel(2) click, right?

Hmmm... off the top of my head, the only way I can think of is to
write a subclass of Label with the clickhandler you want, and then add
those to the controls, rather than the regular Label class.
Or, you could write the click handlers in the main form, and pass a
delegate to each of the labels, and then the label click handlers
could invoke the delegate...

There might be other ways. I haven't tried this. yet ;)
 
O

olds350racer

jayeldee, you're my hero!
That got it. Makes perfect since now. :)

(e-mail address removed), thanks for the input... as you can see, the
delegate idea is the way I went and it worked.

I rack my brain on this stuff too much to call it a hobby. :)
Thanks again all.
 
D

dbahooker

it worked in vb6; they removed it from dotnet

sorry; I just think that visual fred sucks BAD; it is twice as verbose
and 1/4 as much functionality (activeX scripts, vbs, clientside vb,
sql jobs, office vba, etc)
 
Ad

Advertisements

C

CodeMonkey

I am a newbie but have come across a question I hope someone can help
with.
Given the following code for a UserControl:

' Declares in the class
Private intCount as Interger
Private myLabel() as Label
' Code in the Load event of my UserControl
Dim xCount as Integer
intCount = 10
redim myLabel(intCount)
For xCount = 0 to 10
myLabel(xCount) = New Label
Me.Controls.Add(myLabel(xCount))
Next

How would I respond to an event (ie-Click) of one of my myLabel
controls? Given that the control does not exist during the design
phase, there is no event to respond to. I know this sounds like a
newbie question, but I would appreciate if anyone could point me in
the right direction.
[/QUOTE]

Private Sub Label_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Windows.Forms.MessageBox.Show("hi")
End Sub

In your class:

For xCount = 0 To 10
myLabel(xCount) = New Label
AddHandler myLabel(xCount).Click, AddressOf Label_Click
Me.Controls.Add(myLabel(xCount))
Next
 

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