473,396 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating control arrays at runtime

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.

Feb 13 '07 #1
7 1284
On Feb 13, 2:55 pm, "olds350racer" <olds350ra...@bellsouth.netwrote:
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.

Feb 13 '07 #2
I am. I'm adding an array of labels during the Load event of the
UserControl.

Feb 13 '07 #3
On Feb 13, 1:55 pm, "olds350racer" <olds350ra...@bellsouth.netwrote:
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
For xCount = 0 to 10
myLabel(xCount) = New Label
AddHandler myLabel(xCount).Click, AddressOf OhGodSomeoneClickMyLabelPlease
Me.Controls.Add(myLabel(xCount))
Next
Feb 13 '07 #4
On Feb 13, 3:05 pm, "olds350racer" <olds350ra...@bellsouth.netwrote:
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 ;)

Feb 13 '07 #5
jayeldee, you're my hero!
That got it. Makes perfect since now. :)

lord.zol...@gmail.com, 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.


Feb 13 '07 #6
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)


On Feb 13, 11:55 am, "olds350racer" <olds350ra...@bellsouth.net>
wrote:
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.

Feb 15 '07 #7
On Feb 13, 11:55 am, "olds350racer" <olds350ra...@bellsouth.net>
wrote:
>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.

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
Feb 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Venkatesh | last post by:
Hi Everybody, This is the first timw I am entering into this Group. I am developing a VB Project with an MDI form. I want to display IE Favorites into my application. For this I need to...
6
by: owen | last post by:
Generally speaking, what does it mean when I see a "button" with red text showing this message instead of the control I've dragged onto the web form in Design View.? (But the page works fine at...
17
by: Bushido Hacks | last post by:
I've come up with a good set of Matrix codes recently when I cam to a road block. I would like to know if I should destroy an object to create a new object with new dimmensions. class Matrix{...
8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: Ranier Dunno | last post by:
Hi, I'd like to create typed arrays during runtime, sort of like this: Array vals = Array.CreateInstance(objType, noElements); The problem is that objType will not be a "base" type like...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
3
by: krollenhagen | last post by:
Hello. I used vb6 in the past for hobby programming. Now I am using vb.net. I read a bunch of posts on control arrays and read the msdn article...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.