Unable to create a new Label on a Windows Forms Application when clicking a button (C#)
I am trying to add a new label when clicking on a button. Shouldn't be too difficult but I just can't get it working :(
private void button3_Click(object sender, EventA开发者_运维技巧rgs e)
{
Label Label10 = new Label();
Label10.Location = new Point(23, 100);
Label10.Text = textBox1.Text;
Parent.Controls.Add(Label10);
}
I get an error when trying to debug it with a not too explicit message:
"NullReferenceException was unhandled."
Not sure to what it may be referring... I am using the "new" keyword to create the new instance, what may I be missing?
Thanks!
Is Parent
null
? It could be on there too. You might just call this.Controls.Add(...)
.
textbox1
is probably what's throwing that exception. Can you step through your code in the debugger?
精彩评论