How might a programmer work around scope limitations when writing event handling functions involving controls that are not declared via the GUI, but rather in the main source file?
Is it "acceptable" to declare such controls in global scope instead of Form1_Load() to solve this problem?
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?