PDA

View Full Version : use a variable in an object's name


Ludootje
02-01-2004, 12:44 PM
Hi,
What I'd like to do is something like this:


For i = 1 To 10
cmdi.caption = "this is button" & i
Next i

So if I create 10 buttons (cmd1, cmd2, ..., cmd10), the captions of each of them would be changed.

Something like this would also be nice:


Private sub Whatever()
i = txt_a_number.Text
Call DoSomething(i)
End Sub

Private Sub DoSomething(Number As Integer)
cmdNumber.Caption = Number
End Sub


Well you get the point I guess :)
Is it possible to do this?

Thanks.

manual_overide
02-04-2004, 02:49 AM
I'm assuming this is VB6 based on the caption property (.NET does not have this). What you specifically explained is not possible, but what you are getting at is possible if you use a control array.

Create 10 buttons with the name cmdButton. VB will ask you if you want to create a control array. Say yes.


For i = 1 To 10
cmdButton(i).Caption = "This is Button " & i
Next i

Ludootje
02-04-2004, 11:25 AM
Yep, it's VB6. Sorry I forgot to mention this.
I'm using control arrays now - that works perfectly, thanks :)

Just one thing: you said "i = 1 to 10", however it starts at 0 for me, is there a way to make the controls start at another number (1 for example). Not there's anything wrong with 0, just curious :)

Thanks a lot for the help!

manual_overide
02-04-2004, 08:14 PM
probably not. i just used 1 to 10 because that is what you used in the example

Ludootje
02-05-2004, 03:30 PM
Ok thanks a lot, just wondering.

suprize
02-18-2005, 05:08 PM
you can set the indexes to any numbers you want by changing them in the Index property at design time in the properties window. Or if your loading the controls at runtime then you would set the index there

Dim x as Integer
For x = 2 to 10
Load oControl(x)
Next

I started with 2 because loading control arrays at runtime is easier if you added the first one at design time.