PC Review


Reply
Thread Tools Rate Thread

Creating control arrays at runtime

 
 
olds350racer
Guest
Posts: n/a
 
      13th Feb 2007
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.

 
Reply With Quote
 
 
 
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      13th Feb 2007
On Feb 13, 2:55 pm, "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.


I'm a bit confused, you're adding labels to an array?
Probably best to do that in the Load event.

 
Reply With Quote
 
olds350racer
Guest
Posts: n/a
 
      13th Feb 2007
I am. I'm adding an array of labels during the Load event of the
UserControl.

 
Reply With Quote
 
jayeldee
Guest
Posts: n/a
 
      13th Feb 2007
On Feb 13, 1:55 pm, "olds350racer" <olds350ra...@bellsouth.net> wrote:
> 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
>


 
Reply With Quote
 
lord.zoltar@gmail.com
Guest
Posts: n/a
 
      13th Feb 2007
On Feb 13, 3:05 pm, "olds350racer" <olds350ra...@bellsouth.net> wrote:
> 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

 
Reply With Quote
 
olds350racer
Guest
Posts: n/a
 
      13th Feb 2007
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.




 
Reply With Quote
 
dbahooker@hotmail.com
Guest
Posts: n/a
 
      15th Feb 2007
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.



 
Reply With Quote
 
CodeMonkey
Guest
Posts: n/a
 
      15th Feb 2007
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a control at runtime and rendering it to a string pcloches@gmail.com Microsoft ASP .NET 3 3rd Apr 2007 09:16 PM
Multidimensional arrays, get dimensions at runtime xxxxyz@abv.bg Microsoft C# .NET 2 11th Apr 2005 10:48 AM
Creating control arrays at design time. Mark Broadbent Microsoft C# .NET 3 1st Apr 2004 08:08 PM
creating label control arrays =?Utf-8?B?UmFqdQ==?= Microsoft Dot NET 2 18th Mar 2004 03:20 PM
Creating a Windows.Form.Control class for runtime form generation Sinisa Microsoft C# .NET 0 13th Dec 2003 01:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:00 AM.