I am trying to create a dynamic control, based on the
System.Windows.Form.Label control. The program compiles with no errors, but
errors out when trying to access this code:
class MyLabels : Label
{
}
public ucTime()
{
InitializeComponent();
BuildLabels();
}
public void BuildLabels()
{
int xLeft = 20;
int xTop = 20;
int xINC = 34;
for (int i = 0; i < 7; i++)
{
MyLabels[] MyDates = new MyLabels[7];
MyDates[i].Text = "Label " + Convert.ToString(i);
MyDates[i].Width = 70;
MyDates[i].Height = 25;
MyDates[i].BorderStyle = BorderStyle.FixedSingle;
MyDates[i].Left = xLeft;
MyDates[i].Top = xTop;
this.Controls.Add(MyDates[i]);
xTop += xINC;
}
}
with this error:
"use the 'new' keyword to create an object instance, and it fails on the
first line where I'm trying to assign the "Text" value.
Any ideas?
Thanks in advance,
Tom Porterfield - 20 Aug 2007 21:01 GMT
> I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors, but
[quoted text clipped - 36 lines]
>
> Any ideas?
You have (at least) two problems. First, you should create you array of
MyLabels outside of the loop as you only want to do that one time.
Second, while you have created an instance of an array of MyLabel
objects, you have not yet created the MyLabel objects. So I would make
the first line of code inside the loop as follows:
MyDates[i] = new MyLabels();
That should get you going in the right direction.

Signature
Tom Porterfield
sloan - 20 Aug 2007 21:02 GMT
At the very least, you need to put
MyLabels[] MyDates = new MyLabels[7];
outside of your for (i) loop
>I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors,
[quoted text clipped - 39 lines]
>
> Thanks in advance,
Brad Wery - 20 Aug 2007 21:04 GMT
It looks like the array is initialized with with "non instantiated"
MyLabels. And I don't think you should be declaring the array in the loop.
Can you do this instead?
private void button3_Click(object sender, EventArgs e)
{
int xLeft = 20;
int xTop = 20;
int xINC = 34;
for (int i = 0; i < 7; i++)
{
MyLabels MyDates = new MyLabels();
MyDates.Text = "Label " + Convert.ToString(i);
MyDates.Width = 70;
MyDates.Height = 25;
MyDates.BorderStyle = BorderStyle.FixedSingle;
MyDates.Left = xLeft;
MyDates.Top = xTop;
this.Controls.Add(MyDates);
xTop += xINC;
}
}
If not then intantiate the MyLabels class:
MyDates[i] = new MyLabels();
MyDates[i].Text...
Brad
> I am trying to create a dynamic control, based on the
> System.Windows.Form.Label control. The program compiles with no errors, but
[quoted text clipped - 38 lines]
>
> Thanks in advance,