I have a simple button click method which sets a textbox's text to a viarable textFromTB.
private void Button1_Click(object sender, EventArgs e)
{
string textFromTB = TextBox1.Text;
}
If I run that once, its okay, it set the text for TextBox1 to textFromTB. But if I run that method second time it throws an NullReferenceExepction. Why is that?
I tried to initialize textbox like that
private void Button1_Click(object sender, EventArgs e)
{
TextBox TextBox1 = new TextBox();
string textFromTB = TextBox1.Text; // that line returns null
}
but that sets TextBox1.Text
to null or empty string. How can I change the code that it will work multiple times without exceptions?
Thanks in advance!