How to create array of labels with Microsoft Visual C# Express Edition ? Is there way to do it with graphical (drag'n'drop) editor or I have to manually add it to auto generated code ?
+6
A:
You have to manually add it. But don't add it to auto generated code as it can be overwritten by Visual Studio designer.
I would add it in Load event handler for the form. The code can look like this:
Label[] labels = new Label[10];
labels[0] = new Label();
labels[0].Text = "blablabla";
labels[0].Location = new System.Drawing.Point(100, 100);
...
labels[9] = new Label();
...
PS. Your task seems a little unusual to me. What do you want to do? Maybe there are better ways to accomplish your task.
nightcoder
2009-06-07 18:17:30
Thanks... I'm doing Turing Machine simulator. Those labels would represent part of the tape
dpetek
2009-06-07 18:27:07
+3
A:
You can add the labels to the form using the GUI editor, then add those to the array in form load.
Label[] _Labels = new Label[3];
private void MyForm_Load(object sender, EventArgs e)
{
_Labels[0] = this.Label1;
_Labels[1] = this.Label2;
_Labels[2] = this.Label3;
}
This will at least make setting the location easier. Also you might want to consider using the FlowLayoutPanel if you're dynamically creating labels (or any control really).
C. Ross
2009-06-07 18:30:26