“上下文不存在名称”这种问题该怎么解决?

sinat_34072242 2016-02-25 12:10:20
错误的代码:

rivate void button4_Click(object sender, EventArgs e) //form1中的button4控件
{
Form f2 = new Form();
f2.Text = "学籍照片批量下载";
TextBox firstNumberText = new System.Windows.Forms.TextBox();
TextBox lastNumberText = new System.Windows.Forms.TextBox();
Button downloadButton = new System.Windows.Forms.Button();
downloadButton.Text = "批量下载";
f2.Controls.Add(firstNumberText);
f2.Controls.Add(lastNumberText);
f2.Controls.Add(downloadButton);
f2.Show();
downloadButton.Click += new System.EventHandler(downloadButton_Click);
this.Visible = false;
this.Enabled = false;
f2.Visible = true;
f2.Enabled = true;
}
private void downloadButton_Click(object sender, EventArgs e)
{
WebClient myclient = new WebClient();
for (int i = int.Parse(firstNumberText.Text); i < int.Parse(lastNumberText.text); i++)
{
myclient.DownloadFile("http://www.abc.com/" + Convert.ToString(i) + "/.jpg", i + @".jpg");
}
}

错误的原因
问题:上下文怎么会不存在firstNumberText和lastNumberText呢?我都new出来了。求解救
...全文
1010 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sinat_34072242 2016-02-25
  • 打赏
  • 举报
回复
引用 3 楼 ducker3590 的回复:
[quote=引用 2 楼 sinat_34072242 的回复:] [quote=引用 1 楼 ducker3590 的回复:] 因为不在用一个作用域中,你在button4_Click中new的,只能在这个事件里访问,如果想在别的地方访问可以声明称全局的。
静态后button报错更多 [/quote] 你的button4_Click是为了弹出form2吧? 感觉没必要做得这么复杂,你直接在form2上面拖控件,然后写下事件。 之后只要在button4_Click中new一个form2,然后show出来就可以了,接下来的操作都在form2上完成就行了。[/quote] 嗯嗯你的思路是对的,我确实实现了
Justin-Liu 2016-02-25
  • 打赏
  • 举报
回复
建议先学基础
csdnFUCKINGSUCKS 2016-02-25
  • 打赏
  • 举报
回复
引用 2 楼 sinat_34072242 的回复:
[quote=引用 1 楼 ducker3590 的回复:] 因为不在用一个作用域中,你在button4_Click中new的,只能在这个事件里访问,如果想在别的地方访问可以声明称全局的。
静态后button报错更多 [/quote] 你的button4_Click是为了弹出form2吧? 感觉没必要做得这么复杂,你直接在form2上面拖控件,然后写下事件。 之后只要在button4_Click中new一个form2,然后show出来就可以了,接下来的操作都在form2上完成就行了。
sinat_34072242 2016-02-25
  • 打赏
  • 举报
回复
引用 1 楼 ducker3590 的回复:
因为不在用一个作用域中,你在button4_Click中new的,只能在这个事件里访问,如果想在别的地方访问可以声明称全局的。
静态后button报错更多
csdnFUCKINGSUCKS 2016-02-25
  • 打赏
  • 举报
回复
因为不在用一个作用域中,你在button4_Click中new的,只能在这个事件里访问,如果想在别的地方访问可以声明称全局的。

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧