I put a generic list in a private helper method…
private void addButton()
{
List<Button> buttonList = new List<Button>();
buttonList.Add(button1);
buttonList.Add(button2);
buttonList.Add(button3);
buttonList.Add(button4);
buttonList.Add(button5);
buttonList.Add(button6);
buttonList.Add(button7);
buttonList.Add(button8);
buttonList.Add(button9);
}
I know to call this method you have to do something like this…
private void button1_Click(object sender, EventArgs e)
{
addButton();
foreach (button in addButton())
{
button.Enabled = false;
}
}
however… when I try to call button in addButton() red lines appear underneath ‘button’ saying that it could not be found. Please can someone help 🙂
Your method should return the list:
Also you don’t need the first
addButton();:And I would have this method rewritten with yield keyword:
as you do not need to create a list of buttons just to go through them each time you need to. It’s enough just to enumerate them.
Also review Methods (C# Programming Guide), section about methods returning values:
P.S.: review my answer in How do I shorten this generic list?. Probably this would still be a better approach?