|
-
Nov 8th, 2002, 06:57 PM
#1
Thread Starter
Hyperactive Member
Dynamically creating elements of control array **Resolved**
Here is what I have. I have a form with a PictureBox and a vertical scroll bar. On top of the PictureBox I have a OptionButton that is set to graphical type. It is as follows.
optButton(0) 'This is the only optButton control drawn on the form at runtime
It is element 0 of the control array optButton. How can I programatically create the 1,2, and 3 elements of the control array without physically drawing the controls on the form at designtime?
The elements must be created at run-time and only if needed, that way I can create 1 if needed or 100, whatever the case maybe.
Thanks.
Last edited by easymoney; Nov 9th, 2002 at 03:48 AM.
-
Nov 8th, 2002, 07:02 PM
#2
PowerPoster
VB Code:
Dim i%
For i =1 To 3
Load Option1(i)
Option1(i).Move Option1(i-1).Left, Option1(i-1).Top + Option1(i-1).Height
Option1(i).Visible = True
Next i
-
Nov 8th, 2002, 07:04 PM
#3
PowerPoster
You will also need to set caption:
Option1(i).Caption = "Option1" 'or whatever
-
Nov 8th, 2002, 07:20 PM
#4
Thread Starter
Hyperactive Member
I only have one control drawn on the form... Index = 0
So I get this error message:
Control array element '1' does not exist.
This is the heart of my problem. How do you create elements 1,2,3...etc at run-time?
-
Nov 8th, 2002, 07:24 PM
#5
PowerPoster
I just showed you??? Place that code to a Form_Load event and voila ...
-
Nov 9th, 2002, 03:34 AM
#6
Thread Starter
Hyperactive Member
OK... its all in the details, makes sense if you want to create controls, do it when the form loads... duh.
What if you need to redim the control array.... can you force the load event again? (ie, I loaded 20, but now I need 45??)
-
Nov 9th, 2002, 06:03 AM
#7
PowerPoster
VB Code:
Private Sub Command1_Click() 'or from anywhere in the project
Dim i%
For i =Option1.UBound + 1 To Option1.UBound + 25
Load Option1(i)
Option1(i).Move Option1(i-1).Left, Option1(i-1).Top + Option1(i-1).Height
Option1(i).Visible = True
Next i
End Sub
-
Nov 9th, 2002, 06:57 AM
#8
PowerPoster
Here is a quick sample that you may use throughout your project:
VB Code:
'on the form
Option Explicit
Private Sub Command1_Click()
AddMoreControls Option1, 3
End Sub
Private Sub Form_Load()
AddMoreControls Option1, 2
End Sub
'paste this sub into a general module
Public Sub AddMoreControls(ByVal ctl As Object, intNewUbound As Integer)
'========================================================================
Dim i%
For i = ctl.UBound + 1 To ctl.UBound + intNewUbound
Load ctl(i)
ctl(i).Move ctl(i - 1).Left, ctl(i - 1).Top + ctl(i - 1).Height
ctl(i).Visible = True
Next i
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|