[Mono-winforms-list] Creating an array of labels for use later - annoying problem, please help!

Paul paul at all-the-johnsons.co.uk
Tue Sep 15 19:11:07 EDT 2009


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!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
Url : http://lists.ximian.com/pipermail/mono-winforms-list/attachments/20090916/5f4bb61a/attachment.bin 


More information about the Mono-winforms-list mailing list