在编写涉及未通过 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?