By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,264 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,264 IT Pros & Developers. It's quick & easy.

How to Create Object at Runtime

P: n/a
This is probably simple but it has stumped me. I want to create objects at
runtime for example:
A program that would allow you to draw lines on a form. For each new line I
would like to create a new line object in an array the same way you would a
simple variable.

if it was just a variable I could

dim a(0) as single
..
(some code)
..
b=ubound(a)+1
redim preserve a(b) as single
..
(other code)

Is it possible to do this with controls ... say 10 textboxs today 12
tomarrow etc.
right now I put a bunch in an array and hide the ones I don't need.

Thanks
Tom
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Assuming you have one textbox on the form named Text1, with its index
property set to 0 to create a control array:
--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Tom Rathbun" <tj*******@comcast.net> wrote in message
news:jJ********************@comcast.com...
: This is probably simple but it has stumped me. I want to create objects
at
: runtime for example:
: A program that would allow you to draw lines on a form. For each new line
I
: would like to create a new line object in an array the same way you would
a
: simple variable.
:
: if it was just a variable I could
:
: dim a(0) as single
: .
: (some code)
: .
: b=ubound(a)+1
: redim preserve a(b) as single
: .
: (other code)
:
: Is it possible to do this with controls ... say 10 textboxs today 12
: tomarrow etc.
: right now I put a bunch in an array and hide the ones I don't need.
:
: Thanks
: Tom
:
:
Jul 17 '05 #2

P: n/a
grrrrrrr ...

.... use:

'load
Private Sub Command1_Click()

Dim cnt As Long

For cnt = 0 To 25

If cnt > 0 Then Load Text1(cnt)

With Text1(cnt)
.Move 300, 210 + (300 * cnt), 1700
.Visible = True
End With

Next

End Sub

'unload
Private Sub Command2_Click()

Dim cnt As Long

For cnt = 25 To 0 Step -1

If cnt > 0 Then Unload Text1(cnt)

Next

End Sub

You can't unload the control added at design time, so it's important to test
to ensure the index > 0.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Randy Birch" <rg************@mvps.org> wrote in message
news:N3******************@twister01.bloor.is.net.c able.rogers.com...
: Assuming you have one textbox on the form named Text1, with its index
: property set to 0 to create a control array:
:
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://www.mvps.org/vbnet/
: Please respond only to the newsgroups so all can benefit.
:
:
: "Tom Rathbun" <tj*******@comcast.net> wrote in message
: news:jJ********************@comcast.com...
: : This is probably simple but it has stumped me. I want to create objects
: at
: : runtime for example:
: : A program that would allow you to draw lines on a form. For each new
line
: I
: : would like to create a new line object in an array the same way you
would
: a
: : simple variable.
: :
: : if it was just a variable I could
: :
: : dim a(0) as single
: : .
: : (some code)
: : .
: : b=ubound(a)+1
: : redim preserve a(b) as single
: : .
: : (other code)
: :
: : Is it possible to do this with controls ... say 10 textboxs today 12
: : tomarrow etc.
: : right now I put a bunch in an array and hide the ones I don't need.
: :
: : Thanks
: : Tom
: :
: :
:
:
Jul 17 '05 #3

P: n/a
Try this:
Dim line2 As VB.Line
Private Sub Form_Load()

Set line2 = Controls.Add("vb.line", "line2")

line2.X1 = 2520
line2.X2 = 360
line2.Y1 = 400
line2.Y2 = 2000
line2.Refresh
line2.BorderColor = red
line2.Visible = True
' etc etc
End Sub

"Tom Rathbun" <tj*******@comcast.net> wrote in message news:<jJ********************@comcast.com>...
This is probably simple but it has stumped me. I want to create objects at
runtime for example:
A program that would allow you to draw lines on a form. For each new line I
would like to create a new line object in an array the same way you would a
simple variable.

if it was just a variable I could

dim a(0) as single
.
(some code)
.
b=ubound(a)+1
redim preserve a(b) as single
.
(other code)

Is it possible to do this with controls ... say 10 textboxs today 12
tomarrow etc.
right now I put a bunch in an array and hide the ones I don't need.

Thanks
Tom

Jul 17 '05 #4

P: n/a
This is simple,

Create a Line object or TextBox object, set Index to 0, set Visible=False
Put in the example code like below:

Private Sub Form_Load()
Dim i As Integer
For i = 1 To 5
Load Line1(i)
Line1(i).Y1 = i * 100
Line1(i).Y2 = i * 100
Line1(i).Visible = True
Next i
End Sub
Regards,
Joon Nan

"Tom Rathbun" <tj*******@comcast.net> wrote in message
news:jJ********************@comcast.com...
This is probably simple but it has stumped me. I want to create objects at runtime for example:
A program that would allow you to draw lines on a form. For each new line I would like to create a new line object in an array the same way you would a simple variable.

if it was just a variable I could

dim a(0) as single
.
(some code)
.
b=ubound(a)+1
redim preserve a(b) as single
.
(other code)

Is it possible to do this with controls ... say 10 textboxs today 12
tomarrow etc.
right now I put a bunch in an array and hide the ones I don't need.

Thanks
Tom

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.