Change visible all buttons create dynamically in loop if I clicked on certain C#

46 Views Asked by At

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.

0

There are 0 best solutions below