动态添加控件问题
初学C#,写了段代码,在Form1 load时动态添加一个textBox控件和Button控件,然后在Button控件单击事件中用到textBox的text属性,下面是代码,private void Form1_Load(object sender, EventArgs e)
{
TextBox txbox = new TextBox();
txbox.Name = "textBox1";
txbox.Text = "aaa";
txbox.Location = new Point(15,15);
txbox.Size = new Size(150,20);
this.Controls.Add(txbox);
Button btn = new Button();
btn.Text = "sure";
btn.Location = new Point(165,15);
btn.Size = new Size(60,20);
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
void btn_Click(object senedr, EventArgs e)
{
MessageBox.Show(txbox.text);
}
编译时提示: Error 1 The name 'txbox' does not exist in the current context d:\ 我的文档\Visual Studio 2005\Mytest\Mytest\test\Form1.cs 36 29 test
不知道怎么回事啊,怎么找不到textBox控件呢!!!