Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2004

Tip: Looking for answers? Try searching our database.

adding controls at runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tym - 20 Oct 2004 21:57 GMT
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...
Altman - 20 Oct 2004 23:00 GMT
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

>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...
[quoted text clipped - 22 lines]
> Dim sLABEL(I).text  As New Label
> if you know that I mean...
Charlie - 20 Oct 2004 23:11 GMT
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.

> 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...
[quoted text clipped - 22 lines]
>     Dim sLABEL(I).text  As New Label
> if you know that I mean...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.