-
Control array
hello every1
i seem to be having problems adding Control arrays to a Panel. this is how i am doing it,
Code:
Label[] la = new Label[5];
la[0].Text = "label text";
Panel1.Controls.Add(la[0]);
now this compiles fine, but i get an error on runtime. any1 know what im doing wrong?
thanks for your help.
-
You've allocated space for a new array, but you haven't actually allocated space for each item in the array. Try this:
Code:
Label[] labels = new Label[5];
labels[0] = new Label();
labels[0].Text = "Label 1!";
labels[1] = new Label();
labels[1].Text = "Label 2!";
labels[2] = new Label();
labels[2].Text = "Label 3!";
Panel1.Controls.AddRange(labels);
-
thanks for your reply, got it to work perfect :)
-
You're welcome :)
My bill will be in the post.