In my form, I have a panel. In a loop, I create many labels in this panel, but it's too slow.
So, it appears that create a list of labels in the loop and after add them in the panel should be more faster.
Here i my code :
Dim listLabels as new list(of label)
for a=0 to 100
dim lbl as new label
label.name="label" & a.Tostring
listLabels.add(lbl)
next
myPanel.controls.addrange(listLabels)
ERROR: Value of type 'List(of Label)' cannot be converted to 'Control()'
Can you help me please?
thks
Control.Controls
returns a ControlCollection
which AddRange
accepts only a Control()
(array of controls). So it's NOT a List<Control>
which AddRange
accepts IEnumerable<Control>
. You could use ToArray
, it creates an array from the list:
myPanel.Controls.AddRange(listLabels.ToArray())
Since you know the size already and you need an array, you could make it more efficient:
Dim labels(99) as Control ' looks odd, but has place for 100 labels
For a As Integer = 0 To 99
Dim lbl As New Label
lbl.Name = "label" & a.Tostring
labels(a) = lbl
Next
Label1.Controls.AddRange(labels)