f



VB5 - How to add "labels" to a "collection" at runtime?

Hello,

I tried to add a variable number of identical "labels" on a form at runtime,
using the Collection.Add method.
A previous designed label on the same form is used as model. The following
code ends at i=1 with a "wrong type" error:

Dim Vignette As New Collection
Dim iC As Label
Set iC = Txt        ' the existing "model"
For i = 1 To ncar    ' loop to add the labels
    Vignette.Add iC, i    ' <<< error '13' on this line

...... other stuff ..
Next i
Set iC = Nothing

What is wrong ?

Thanks in advance.



0
4/26/2004 10:17:10 AM
comp.lang.basic.visual.misc 10153 articles. 0 followers. Post Follow

5 Replies
356 Views

Similar Articles

[PageSpeed] 35

"Albert BOLLE" <Albert.Bolle@ulg.ac.be> wrote in message
news:c6injq$le$1@ikaria.belnet.be...

> I tried to add a variable number of identical "labels" on a form
> at runtime, using the Collection.Add method.
> A previous designed label on the same form is used as model.
> The following code ends at i=1 with a "wrong type" error:
> Dim Vignette As New Collection
> Dim iC As Label
> Set iC = Txt        ' the existing "model"
> For i = 1 To ncar    ' loop to add the labels
> Vignette.Add iC, i    ' <<< error '13' on this line
> Next i

Works okay here. Check that your original Label actually does have "txt" as
its Name property. Also check (more likely) that you have not created some
other variable with the same name (for example Dim txt as String).

By the way, why are you using a Collection rather than a simple Control
Array of Labels?

Mike



0
mike5607 (1952)
4/26/2004 11:43:31 AM
"Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message
news:c6isl9$n54$1@newsg2.svr.pol.co.uk...

> Works okay here. Check that your original Label actually does have
> "txt" as its Name property. Also check (more likely) that you have
> not created some other variable with the same name (for example
> Dim txt as String).

That's odd! It worked before and now it doesn't! Mind you, I never use
Collections so I don't know (and don't want to know!) much about them. Why
not use an array instead? It is simple to set up and it allows you to easily
write code to handle events for the individual elements of the array in one
little block. First (in the IDE) click your Label that is called "txt" in in
the properties window click into its Index property and enter the value 0
(zero) then click in its Backstyle property and set it to Transparent. Then
you can write code like:

Dim ncar As Long, i As Long, x As Long, y As Long
ScaleMode = vbPixels: ncar = 5
For i = 1 To ncar
Load Txt(i)
Txt(i).Visible = True
Txt(i).Move x, y
x = x + 20: y = y + 20
Next i

Mike





0
mike5607 (1952)
4/26/2004 12:09:09 PM
"Albert BOLLE" <Albert.Bolle@ulg.ac.be> wrote in message news:<c6injq$le$1@ikaria.belnet.be>...
> Hello,
> 
> I tried to add a variable number of identical "labels" on a form at runtime,
> using the Collection.Add method.
> A previous designed label on the same form is used as model. The following
> code ends at i=1 with a "wrong type" error:
> 
> Dim Vignette As New Collection
> Dim iC As Label
> Set iC = Txt        ' the existing "model"
> For i = 1 To ncar    ' loop to add the labels
>     Vignette.Add iC, i    ' <<< error '13' on this line

well, even if it worked it would not do what you seem to want.  That
would, at best, add ncar references to the same label to the
collection so a change in any one of them would change all of them
(since they'd all actually be the same label object).  If you want to
add additional labels you have 2 options:

set the Index property of your "model" to 0 and then use the Load and
Unload statements to manage a control array

use the Controls.Add statement to create additional labels

Either way you do not need your own collection object
0
butlerbob (241)
4/26/2004 1:45:24 PM
"Albert BOLLE" <Albert.Bolle@ulg.ac.be> wrote in message
news:c6injq$le$1@ikaria.belnet.be...
> Hello,
>
> I tried to add a variable number of identical "labels" on a form at
runtime,
> using the Collection.Add method.
> A previous designed label on the same form is used as model. The following
> code ends at i=1 with a "wrong type" error:
>
> Dim Vignette As New Collection
> Dim iC As Label
> Set iC = Txt        ' the existing "model"
> For i = 1 To ncar    ' loop to add the labels
>     Vignette.Add iC, i    ' <<< error '13' on this line
>
> ..... other stuff ..
> Next i
> Set iC = Nothing
>
> What is wrong ?
>
> Thanks in advance.
>
I am mystified why you would want to do what you are doing, however,  this
part will error:

For i = 1 To ncar    ' loop to add the labels
     Vignette.Add iC, i  'index to a collection needs to be a string
    'Vignette.Add iC, Cstr(i) will work



0
harry8855 (7)
4/26/2004 9:09:11 PM
"Mike Williams" <Mike@WhiskyAndCoke.com> a �crit dans le message de
news:c6iu54$5lh$1@news5.svr.pol.co.uk...
> "Mike Williams" <Mike@WhiskyAndCoke.com> wrote in message
> news:c6isl9$n54$1@newsg2.svr.pol.co.uk...
>
> > Works okay here. Check that your original Label actually does have
> > "txt" as its Name property. Also check (more likely) that you have
> > not created some other variable with the same name (for example
> > Dim txt as String).
>
> That's odd! It worked before and now it doesn't! Mind you, I never use
> Collections so I don't know (and don't want to know!) much about them. Why
> not use an array instead? It is simple to set up and it allows you to
easily
> write code to handle events for the individual elements of the array in
one
> little block. First (in the IDE) click your Label that is called "txt" in
in
> the properties window click into its Index property and enter the value 0
> (zero) then click in its Backstyle property and set it to Transparent.
Then
> you can write code like:
>
> Dim ncar As Long, i As Long, x As Long, y As Long
> ScaleMode = vbPixels: ncar = 5
> For i = 1 To ncar
> Load Txt(i)
> Txt(i).Visible = True
> Txt(i).Move x, y
> x = x + 20: y = y + 20
> Next i
>
> Mike
>
>
Unlike the others, this solution works perfectly. Thank you !


0
4/28/2004 12:45:50 PM
Reply: