Reputation: 193
I was writing a program which created labels for writing text on a form, but after somehow managing to destroy an important class, I started again from scratch... I can't seem to make it print labels anymore though, they simply don't appear.
public partial class Form1 : Form
{
static Form FormX = new Form();
public Form1()
{
Shown += new EventHandler(FormX_Shown);
InitializeComponent();
}
public void FormX_Shown(object sender, EventArgs e)
{
WriteTextOnScreen("Hello!");
}
public void WriteTextOnScreen(string text)
{
Label tempLabel = new Label();
tempLabel.Text = text;
tempLabel.Name = "";
tempLabel.Location = new Point(10, 10);
FormX.Controls.Add(tempLabel);
}
}
I'm not sure what the problem is, but it's becoming incredibly more annoying by the moment because I'm not smart enough to fix it on my own :-P
Upvotes: 0
Views: 749
Reputation: 216253
You add the labels to the controls collection of FormX and this form is never shown.
I think you should add the labels to you own instance (this)
public void WriteTextOnScreen(string text)
{
Label tempLabel = new Label();
tempLabel.Text = text;
tempLabel.Name = "";
tempLabel.Location = new Point(10, 10);
this.Controls.Add(tempLabel);
}
However this will work for just one label because the location is always the same (10,10). If you call this method more than once the last label will be drawn above the previous one and you will see only the last one.
Upvotes: 1