0

I've got this problem: In the loop I create buttons:

 for (int i = 0; i < poz; i++) { for (int j = 0; j < pion; j++) { Button button = new Button(); button.Size = new Size(30, 30); button.Location = new Point(30 * (i + 1), 30 * (j + 1) + 50); button.Click += new EventHandler(button_Click); Controls.Add(button); } } 

And when I clicked on button, it change visible to false:

 protected void button_Click(object sender, EventArgs e) { Button button = sender as Button; button.Visible = false; } 

But finally, I want to achieve when if I clicked on certain button, all the buttons in this loop change visible to false. My condition is:

 for (int k = 0; k < m; k++) { if (x[k] == i && y[k] == j) { //here buttons change visible to false } } 

...When m is count of elements in x and y arrays, and i is number of buttons horizontally and j is number of buttons in vertical.

I tried using a variable that determines if the button was clicked and many different methods, but none worked. I expect a simple solution.

2
  • 1
    Have you tried adding the buttons to a list? Commented Jul 1, 2023 at 2:33
  • You need to have the Buttons in some sort of list so that you can loop over that to access each of them. If the form structure allows it, one option could be to add a Panel to the form and then add the Buttons to the Panel instead of directly to the form. You can then loop over all the Buttons in the Controls collection of that Panel. If that's not possible or not preferable, just use a List<Button>. Commented Jul 1, 2023 at 2:45

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.