all groups > vb.net upgrade > october 2004 >
You're in the

vb.net upgrade

group:

adding controls at runtime



RE: adding controls at runtime Charlie
10/20/2004 3:11:02 PM
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]
Re: adding controls at runtime Altman
10/20/2004 5:00:55 PM
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]

adding controls at runtime Tym
10/20/2004 9:57:57 PM
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...

Re: adding controls at runtime Tym
10/21/2004 7:51:49 PM
On Wed, 20 Oct 2004 17:00:55 -0500, "Altman" <NotGiven@SickOfSpam.com>
[quoted text, click to view]


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

Re: adding controls at runtime Tym
10/21/2004 7:53:39 PM
On Wed, 20 Oct 2004 15:11:02 -0700, "Charlie"
[quoted text, click to view]

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]

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...

AddThis Social Bookmark Button