vb.net upgrade:
There is no inherent index property for controls. Here is a piece of code
that brings the old VB6 way of doing things into an object oriented
environment: This would go into the Load event of the form.
*****start code******
Dim i As Int32
Dim LBL As Label
Dim AL_Labels As New ArrayList()
For i = 0 To 9
LBL = New Label()
With LBL
.Location = New Point(100, i * 24)
.Size = New Size(100, 20)
.Name = "LBL_" & i.ToString
.Text = "Label " & i.ToString
End With
Me.Controls.Add(LBL)
AL_Labels.Add(LBL)
Next
LBL = DirectCast(AL_Labels(7), Label)
MessageBox.Show(LBL.Text, LBL.Name)
******end of code*******
You would probably put AL_Labels outside the Load event and give it the
scope you need.
[quoted text, click to view] "Tym" wrote:
> I was looking for a way to add a control array at runtime - but it
> doesn't look like vb.net has that ability any more...
>
> Was looking at controls one at a time thus:
>
> Dim Label1 As New Label
> Label1.Text = "label 1"
> Label1.Location = New Point(20, 25)
> Me.Controls.Add(Label1)
>
> Is there any easy way of doing domething like.....
>
> For I = 1 to 100
> Dim Label(I) As New Label
> Label(I)1.Text = "label " & I
> Labe(i)1.Location = New Point(20 * I, 25)
> Me.Controls.Add(Label(I))
> Next I
>
> of perhaps dynamically naming the variable in the dim statment
>
>
> for I = 1 to 100
> sLabel(i) = "LABLE_NO_" & I
> next I
> Dim sLABEL(I).text As New Label
> if you know that I mean...
>
>
You are somewhat close. Declare an array of labels first
dim myLabel(100) as label
Then do the for loop and create new instances of the label class and store
them in the array
For I = 1 to 100
myLabel(I) = new label
myLabel(I).Text = "Whatever"
Me.Controls.Add(Label(I))
Next I
[quoted text, click to view] "Tym" <no_spam@ictis.net> wrote in message
news:s2kdn0dfsljh9buknfjrrb1rg80atkpekk@4ax.com...
>I was looking for a way to add a control array at runtime - but it
> doesn't look like vb.net has that ability any more...
>
> Was looking at controls one at a time thus:
>
> Dim Label1 As New Label
> Label1.Text = "label 1"
> Label1.Location = New Point(20, 25)
> Me.Controls.Add(Label1)
>
> Is there any easy way of doing domething like.....
>
> For I = 1 to 100
> Dim Label(I) As New Label
> Label(I)1.Text = "label " & I
> Labe(i)1.Location = New Point(20 * I, 25)
> Me.Controls.Add(Label(I))
> Next I
>
> of perhaps dynamically naming the variable in the dim statment
>
>
> for I = 1 to 100
> sLabel(i) = "LABLE_NO_" & I
> next I
> Dim sLABEL(I).text As New Label
> if you know that I mean...
>
>
On Wed, 20 Oct 2004 17:00:55 -0500, "Altman" <NotGiven@SickOfSpam.com>
[quoted text, click to view] wrote:
>You are somewhat close. Declare an array of labels first
>dim myLabel(100) as label
>
>Then do the for loop and create new instances of the label class and store
>them in the array
>
>For I = 1 to 100
> myLabel(I) = new label
> myLabel(I).Text = "Whatever"
> Me.Controls.Add(Label(I))
>Next I
Thanks. worked a treat - the actual code used is:
Dim lblDISPLAY(20) As Label
For iLOOP = 1 To 20
lblDISPLAY(iLOOP) = New Label
lblDISPLAY(iLOOP).Text = "Label No " & iLOOP.ToString
lblDISPLAY(iLOOP).Location = New Point(100, iLOOP * 20)
Me.Controls.Add(lblDISPLAY(iLOOP))
Next iLOOP
On Wed, 20 Oct 2004 15:11:02 -0700, "Charlie"
[quoted text, click to view] <Charlie@discussions.microsoft.com> wrote:
>There is no inherent index property for controls. Here is a piece of code
>that brings the old VB6 way of doing things into an object oriented
>environment: This would go into the Load event of the form.
I thought of a control array as I was stil thinking in terms of VB6!
I've managed to get the "display" I need - see other post.
[quoted text, click to view] >
>*****start code******
>
>Dim i As Int32
> Dim LBL As Label
> Dim AL_Labels As New ArrayList()
> For i = 0 To 9
> LBL = New Label()
> With LBL
> .Location = New Point(100, i * 24)
> .Size = New Size(100, 20)
> .Name = "LBL_" & i.ToString
> .Text = "Label " & i.ToString
> End With
> Me.Controls.Add(LBL)
> AL_Labels.Add(LBL)
> Next
>
> LBL = DirectCast(AL_Labels(7), Label)
> MessageBox.Show(LBL.Text, LBL.Name)
>
>******end of code*******
>
>You would probably put AL_Labels outside the Load event and give it the
>scope you need.
Thanks - I'll look at this in more depth and compare to the code
derived from Altman's post.
I'm not too sure what you mean by the last sentence though...