I have a grid of labels. In order to access them in a programmatically sensible manner, I changed them from:
gridLabel1
gridLabel2
...
gridLabelN
To:
gridLabel[0]
gridLabel[1]
...
gridLabel[N-1]
But now the designer is complaining that
The variable ‘gridLabel’ is either undeclared or was never assigned.
Despite the fact that I also changed this:
private System.Windows.Forms.Label gridLabel1 = new System.Windows.Forms.Label;
private System.Windows.Forms.Label gridLabel2 = new System.Windows.Forms.Label;
...
private System.Windows.Forms.Label gridLabelN = new System.Windows.Forms.Label;
To:
private System.Windows.Forms.Label[] gridLabel = new System.Windows.Forms.Label[N];
What have I missed?
Don’t mess around with generated code yourself. The
designer.csfile is supposed to be managed by the forms-designer itself, not by the user. In particular, it’s easy to test that the designer does not tolerate the procedure you appear to be following:InitializeComponentmethod.When I do so, I get the same error that you do.
The usual solution for this sort of requirement is to use a programmatic technique to add a number of controls to a parent-control’s control-collection. Ideally, create your own user-control that can hold a number of
Labels(which it populates programmatically). Then, you can use this control on your form with full designer support.EDIT:
For example (sketch only, without the user-control), add a
FlowLayoutPanelto the form via the designer.Then change the form’s constructor to:
Obviously, such a solution may not be appropriate if you don’t want the labels organized in a flow-layout.