【发布时间】:2013-06-22 17:11:06
【问题描述】:
在编写涉及未通过 GUI 声明而是在主源文件中声明的控件的事件处理函数时,程序员如何绕过范围限制?
在全局范围内声明此类控件而不是 Form1_Load() 来解决此问题是否“可以接受”?
private void Form1_Load(object sender, EventArgs e)
{
ComboBox t = new ComboBox();
Button b = new Button();
b.OnClick += b_OnClick;
}
private void b_OnClick(object sender, OnClickEventArgs e)
{
s.Add("Hello s!"); // The object s is a ComboBox control generated in the Designer GUI
t.Add("Hello t!");
}
// Line 10 is valid.
// Line 11 is invaid because t does not exist in the current scope. How might one work around this issue?
【问题讨论】:
-
这很好,因为对控件的引用已添加到表单的
Controls,但是要引用其中一个控件,除了在其中查找之外别无他法您表单的Controls。这不是很方便。
标签: c# .net winforms scope controls