control array

usetheforce2
09-14-2000, 06:54 PM
two questions
how can i create a control array (say of command buttons) at run time. the following code will produce two command buttons, however it is not a control array.

Option Explicit
Dim MyControl As CommandButton

Private Sub Form_Load()
Dim i As Integer
For i = 1 To 2
Set MyControl = Form1.Controls.Add("vb.commandbutton", "command" & i)
MyControl.Visible = True
If i = 2 Then
MyControl.Left = MyControl.Width + 100
End If
Next
End Sub

this will create "Command1" and "Command2" side by side: what i want to be able to do is have and array of command buttons such as :
Command(1)'indexs
Command(2)'indexs

my second question, when i create these to controls how can i enter a sub. such as :
Private sub Command()_click(index as integer)

thanks in advance for any Help!

Regan DeDiana

BillSoo
09-14-2000, 07:04 PM
To add a control array, I usually have the first control already on the form. For instance, you can have one command button on the form and set it's index property to 0. Then at run time, you can dynamically add more buttons as follows:

load command1(1) 'loads command1 button, index 1 to the form
command1(1).move x,y 'move to wherever
command1(1).caption = "new name"
command1(1).visible = true 'since it is invisible by default

This also solves your second question since you would put the event code in the normal button click event of the initial button.

If you didn't want any buttons on the form at start, you could make the initial button invisible.

"I have a plan so cunning you could put a tail on it and call it a weasel!" - Edmund Blackadder

usetheforce2
09-14-2000, 08:46 PM
that's just beautiful, thanks man!
it's almost too simple!

Regards,

cbrewer
09-15-2000, 12:14 AM
arrays are great,

Here is a simple sample that takes advantage of the count property

Private Sub Form_Load()
Command1(0).Caption = "Button 1"
End Sub


Private Sub Command1_Click(Index As Integer)
Load Command1(Command1.Count)

With Command1(Command1.Count - 1)
.Top = Command1(Command1.Count - 2).Top + Command1 _
(Command1.Count - 2).Height
.Left = Command1(Command1.Count - 2).Left
.Caption = "Button " & Command1.Count
.Visible = True
End With

End Sub





Chuck

usetheforce2
09-15-2000, 05:10 PM
cbrewer, thanks for the idea/tip. creating control arrays and such is a new thing for me, and all of the hints that i can get, come in handy.

Ps
if there are any more cbrewer's that have some tips or advice on this topic, please feel free to poke them in.

Regards,
Regan

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum