Populating labels from an array

I

Ian

I have 5 labels on my form called Label1 to Label5.
I wish to populate each label with data stored in a simple string array.

e.g.

For i = 0 to 4
Label(i).text = array(i)
Next i

In VB6 I simply used a control array, but in .Net all controls require a
unique name. How would I write the above in .Net?

Ian
 
C

Cor

Hi Ian,

For normal purpose (not dynamicly making controls) you don't need the
control array. You can loop through controls.

Keep in mind that a control is always a child of his parent.

I take your example and make it the same in a for each loop.
I know this sounds a little bit strange and you can think on a control
array,
on the other hand here you have an example how to do that without it when
your labels (childs) are direct on the form (parent)

This example is very quick and dirty written so watch typos and small errors
\\\
dim ctr as control
for each ctr in me.controls
if typof ctr is label then
if ctr.name.substring(0,5) = "label" then
ctr.text = array(ctr.name.substring(5,1))
end if
end if
next
///

Of course when you have more labels with the name label, you should test
first if the labelname was between label1 and label5
 

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