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

Member Avatar for stbuchok

Should you use buttons, or checkboxes?

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

guys need help please if there is any query i can explain it

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;                        
                }
            }

           
        }

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.

commented: very well explained +2

thanks man you really make a lot things clear to me thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.