Hi,
i have 2 buttons btn1 and btn2 when i click btn1 it creates an array of buttons Now i want to call those buttons when i click btn2

its bit complicated project (for me) i cant explain it i just want to add "if" check in btn2 to check whether the array buttons are on or off

also tell me how to change the Accessmodifier of button in codes and will it b helpful if i change the modifier of those array buttons

Edited 5 Years Ago by de Source: n/a

i have a 2D int array of same size as array buttons i just want to check if array button is pressed then i would have "1" else "0" in that 2D array

i would be using array button in this manner

Edited 5 Years Ago by de Source: n/a

Where are you storing the references to the array of buttons? Do you want to be able to click on those buttons? Please explain, exactly, what you want to have happen when you click on button2.

here is my in which array of buttons are formed

public void btnAddButton_Click(object sender, EventArgs e)
        {   
            int Row = Convert.ToInt32(tbRows.Text);
            int Column = Convert.ToInt32(tbColumns.Text);
            int Width = Convert.ToInt32(tbWidth.Text);
            int Height = Convert.ToInt32(tbHeight.Text);
            Button[,] buttons = new Button[Column, Row];            
            for (int i = 0; i < Column; i++)
            {
                for (int j = 0; j < Row; j++)
                {
                    buttons[i,j] = new Button();                   
                    panel1.Controls.Add(buttons[i, j]);                    
                    buttons[i,j].Location = new Point(buttons[i,j].Location.X + i * Width, buttons[i,j].Location.Y+j*Height);
                    buttons[i,j].Name = "btn" + i + "," + j;
                    buttons[i,j].Click += new System.EventHandler(ClickButton);
                    buttons[i,j].Size = new Size(Width, Height);                   
                }
            }            
        }

here is a code where i want to call those array of button and place a check on them

private void btnFramCode_Click(object sender, EventArgs e)
        {
            int Row = Convert.ToInt32(tbRows.Text);// why i have to declare and initialize it again as i have declare it in that button can't i call it from there?
            int Column = Convert.ToInt32(tbColumns.Text);

            int[,] arr = new int[Column, Row];
            for (int i = 0; i < Column; i++)
            {
                for (int j = 0; j < Row; j++)
                {
                        if ()// i have to call buttons[i,j] and place a check whether this button' back colour is red or not 
                        arr[i,j] = 1;                        
                }
            }

           
        }

Edited 5 Years Ago by de Source: n/a

To answer the comment in the second block of code, you have to declare and initialize it again because of something called 'scope'. The variables declared in one method are not available after that method is over unless there is a reference to that variable stored somewhere.

For the same reason, your array of buttons (buttons[,]) is no longer available since it was declared inside another method. They don't just vanish because you have added them to the collection of another control (thus there are still references to them).
If you move the declaration of buttons[,] outside of the method, it will be available to the 2nd method, which pretty much solves your problem. Moving it outside the method makes it an instance variable, which means it's available to all the methods of that instance of the class.

Comments
very well explained
This question has already been answered. Start a new discussion instead.