select label dynamically

G

gerryR

Hi All

I have an app that has 64 labels and an array with 64 members. I'm using a
ranom generator to pick a number and put it in the array but then I need to
alter the text of the corresponding label.

So far I've been unable to get the correct label selected based on the
number generated. I'm new to programming so apologies if this is a simple
task, I'd appreciatea point in the right direction.

e.g. of what I'm trying to do:

Dim var1, var2, var3 As Label
Dim i As Short = CInt(Int((4 * Rnd()) + 1))

var(i).Text = "X"

Is this possible to get this working?

TIA
gr
 
T

Teemu

gerryR said:
Hi All

I have an app that has 64 labels and an array with 64 members. I'm using
a ranom generator to pick a number and put it in the array but then I need
to alter the text of the corresponding label.

If you have those labels created in design mode, you could try this:

Dim RandomNumber As New Random
Dim LabelArray As Label() = {Label1, Label2, Label3, Label4}
LabelArray(RandomNumber.Next(0, 3)).Text = "X"

-Teemu
 
E

eBob.com

gerryR said:
Hi All

I have an app that has 64 labels and an array with 64 members. I'm using
a ranom generator to pick a number and put it in the array but then I need
to alter the text of the corresponding label.

So far I've been unable to get the correct label selected based on the
number generated. I'm new to programming so apologies if this is a simple
task, I'd appreciatea point in the right direction.

e.g. of what I'm trying to do:

Dim var1, var2, var3 As Label
Dim i As Short = CInt(Int((4 * Rnd()) + 1))

var(i).Text = "X"

Is this possible to get this working?

TIA
gr
Yes, it's possible to get it working. You'll have to learn a bit of
programming first. The example code you posted does not compile because you
are using "var" as an array but you haven't defined an array named "var" -
you may think of var1, var2 and var3 as an array but VB.Net does not. If
you change your "Dim var1, var2, var3 As Label" statement to "Dim var(63) as
Label" then you will at least have code which compiles. Note that since
subscripts in VB.Net start at 0, not 1, var(63) does provide an array of 64
elements.

With that change your code will compile but will still not execute, i.e. it
will "throw an exception". I could tell you why but I don't want to deprive
you of a great learning experience. Really. You will learn programming only
by making many mistakes and figuring them out. But here's some information
you'll need before you get this code working. "Dim var(63) as Label" does
not create 64 Labels. It creates 64 REFERENCES TO Labels. You'll have to
actually create a Label before you can set its Text property.

Bob
 
C

Cor Ligthert[MVP]

Little addictions.

It creates 64 labels because to keep it compatible with VB versions before
VB6. This was the result of intensive lobying from VB6 developpers. You can
now use the zero as start of an index and the first (like VB6) as start of
an index.

(One of the most confusing and stupid things in VB in my idea).

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top