Create class from known control fails

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.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;
}

}

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,
 
vbtrying said:
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.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;
}

}

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?


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 = new MyLabels();

That should get you going in the right direction.
 
At the very least, you need to put

MyLabels[] MyDates = new MyLabels[7];

outside of your for (i) loop
 
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 = new MyLabels();
MyDates.Text...


Brad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top