c# 数组调用问题~急~

来源:百度知道 编辑:UC知道 时间:2024/06/22 19:25:25
窗体自动加载事件
public void Book_Load(object sender, EventArgs e)
{
TextBox[] mytextbox = new TextBox[9];
mytextbox[0] = this.textBox1;
mytextbox[1] = this.textBox2;
mytextbox[2] = this.textBox3;
mytextbox[3] = this.textBox4;
mytextbox[4] = this.textBox5;
mytextbox[5] = this.textBox6;
mytextbox[6] = this.textBox7;
mytextbox[7] = this.textBox8;
mytextbox[8] = this.textBox9;
}
然后我在一个按钮事件中想调用上面窗体事件里的数组
private void next_Click(object sender, EventArgs e)
{
mytextbox[0].Text = ds.Tables["book"].Rows[0][0].ToString();
}
可是提示说 "当前上下文不存在名称mytextbox" 是什么意思?

我只剩下5分了 各位帮帮忙啊~~

mytextbox 是在Book_Load方法里面声明的,所以只在这个方法里面能看到
可以把它弄到类里面去声明,就是放在Book_Load方法的外面,但是实例化是在方法里面,大概是
TextBox[] mytextbox;
public void Book_Load(object sender, EventArgs e)
{
mytextbox = new TextBox[9];
mytextbox[0] = this.textBox1;
mytextbox[1] = this.textBox2;
mytextbox[2] = this.textBox3;
mytextbox[3] = this.textBox4;
mytextbox[4] = this.textBox5;
mytextbox[5] = this.textBox6;
mytextbox[6] = this.textBox7;
mytextbox[7] = this.textBox8;
mytextbox[8] = this.textBox9;
}

声明的地方放的不对!,页面装载时定义的,在按钮调用的时候无效

public void Book_Load(object sender, EventArgs e)
{

}
然后我在一个按钮事件中想调用上面窗体事件里的数组
private void next_Click(object sender, EventArgs e)
{
TextBox[] mytextbox = new TextBox[9];
mytextbox[0] = this.textBox1;
mytextbox[1] = this.textBox2;
mytextbox[2] = this.textBox3;
mytextbox[3] = this.textBox4;
mytextbox[4] = this.textBox5;
mytextbox