Creating an array of labels for use later - annoying problem, please help!

Paul <[email protected]>
Newsgroups gmane.comp.gnome.winforms
Organization Home
Message-ID <[email protected]>
Hi,

Got a fun one here.

In the Designer file, I have defined my array of labels like this

System.Windows.Forms.Label[] periodlabel = new
System.Windows.Forms.Label[10];

then at the bottom

private System.Windows.Forms.Label[] periodlabel;

The reason for this is that I want to be able to create on the between 1
and 10 labels. The problem is this...

My source looks like this

public partial class lessonplan : Form
{
    public lessonplan(int noperiods)
    {
	float periodSize = 100.0F / (float)noperiods;
	InitializeComponent();
	this.lesson.ColumnCount = noperiods + 1;
	this.lesson.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute,
120F));
	for (int i = 0; i < noperiods; ++i)
	{
		this.lesson.ColumnStyles.Add(new
System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent,
periodSize));
		this.periodlabel[i].Location = new System.Drawing.Point(127 + (117 *
i), 2);
           	this.periodlabel[i].Text = "Lesson " + i.ToString();
		this.periodlabel[i].AutoSize = true;
		this.periodlabel[i].Font = new System.Drawing.Font("Arial", 18F,
System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point,
((byte)(0)));
		this.periodlabel[i].Size = new System.Drawing.Size(109, 27);
		this.periodlabel[i].TabIndex = i + 6;
this.lesson.Controls.Add(this.periodlabel[i], 0, i);
	}
}

The source compiles fine (well, it tells me that Field
'timetabler.lessonplan.periodlabel is never assigned to, and will always
have its default value null, but from what I've read, that's ok) , but
when I run the application and click on the button which instantates the
lessonplan class (it is passing a value happily, if I remove the
periodlabel code, everything works find), I get 

System.NullReferenceException: Object reference not set to an instance
of an object

I've tried a number of solutions to this (including putting a
periodlabel[i] = new Label(); just before the addition of the
periodlabel values but within the braces, trying to create dummy
instances in the designer source file and a few other tricks found
online - nothing worked).

Is there a nice, easy way of doing what I want here?

TTFN

Paul

-- 
Sie können mich aufreizen und wirklich heiß machen!

_______________________________________________
Mono-winforms-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-winforms-list
signature.asc (application/pgp-signature, 198 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iEYEABECAAYFAkqwHwoACgkQaomOUCNPh0IBdgCggFqmS6vEpagfwzafhbCFNzEW
MJ0AnA+MjjBlGLGvIpDXcxz7cKKyL5Wt
=RC4g
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.