Finding controls from an event handler
Suppose I create a System.Windows.Form object within a function, and place a textbox and button on it. I then add a function to handle a click event for the button. How can I get access to the textbox from the event handler without declaring a class-wide variable? Ie:
Code:
namespace Foo
{
class Bar
{
static void Main()
{
Form form=new Form();
TextBox text=new TextBox();
Button button=new Button();
button.Click+=new EventHandler(ButtonClick);
form.Controls.Add(text);
form.Controls.Add(button);
}
private void ButtonClick(object sender, EventArgs e)
{
//How do I get access to "text" here?
}
}
}
Note that I do not want to make a new class for the form.
How can I safely access the "text" object?