BackDoorNoBaby
BackDoorNoBaby

Reputation: 1455

How to clear Button Controls from a Form

My form is blank except for a Menu Strip at the top for New Game and Exit. When the form loads, it is populated with a grid of buttons whose dimensions are read from an App.config file. What is the best way to clear all of the buttons that have been placed on the form for a new game?

this.Controls.Clear();

The above doesn't work because it deletes the Menu Strip at the top of the Form as well and it only lets me call it from the Form. I also have a function that I run within the Gameboard class for the program

    public void ClearButtons()
    {

        for (int q = 0; q < buttonArray.Length; q++)
        {
            buttonArray[q].Text = "";
            buttonArray[q].Enabled = true;
            buttonArray[q].BackColor = Color.LightGray;
        }
    }

But there has to be a better way to do this, if I could just re-load the form, it would re-populate it with the buttons needed but I can't seem to figure that out, especially from my Gameboard class. Thanks all.

Upvotes: 0

Views: 5251

Answers (4)

sagar jadhav
sagar jadhav

Reputation: 1

int n = 0;
while (n < number)
{
    btnArray[n].Name = "" + (n + 1);
    tablePanel.Controls.Remove(btnArray[n]);
    n++;
}

Upvotes: -1

VladL
VladL

Reputation: 13033

1) Instead of clear you can use following code which will remove all buttons:

  public void ClearButtons()
  {
    for (int i = 0; i < this.Controls.Count; i++)
    {
       if (Controls[i] is Button)
       {
         Controls.RemoveAt(i);
         i--;
       }
    }
  }

2) If you want to call this code from another class, you must pass the instance of your form to it, for example as constructor parameter

3) Create public void MyInits() function and move the code from Form1_Load there. Then just call it from Form1_Load

4) Edit constructor to

public Gameboard(int numberofButtons, Form1 frm)

and pass this then instantiating it. Than call frm.MyInits(); in the constructor

Upvotes: 2

Tony Hopkinson
Tony Hopkinson

Reputation: 20320

Instead of using this.Controls.Clear

You could do loop through this.Controls, and test to see if it was a Button and then remove it.

A better way though, would be to create a container, panel or some such, and create your buttons inside that and use

this.Controls.Remove(myPanel);
myPanel.Dispose();

Upvotes: 0

BackDoorNoBaby
BackDoorNoBaby

Reputation: 1455

So this is my Gameboard constructor and Ill show you how the Form calls it to populate with buttons at the bottom.

public Gameboard(int numberofButtons) //Constructor method that is referencing the App.config for the dimensions value to make the board
    {
        if (numberofButtons <= 0) 
            throw new ArgumentOutOfRangeException("Invalid Grid"); //throws an exception for an invalid grid size if dimensions is equal to or less than zero

        buttonArray = new Button[numberofButtons]; //creating an array the size of numberofButtons which is the dimensions value from App.config
        Font font = new Font("Times New Roman", 36.0f); //creates an instance of the Font class
        int sqrtY = (int) Math.Sqrt(numberofButtons);
        int z = 0; //Counter for array

        //Create the buttons for the form
        //Adds the buttons to the form first with null values then changes the .Text to ""
        for (int x = 0; x < sqrtY; x++)
        {
            for (int y = 0; y < sqrtY; y++)
            {
                buttonArray[z] = new Button();
                buttonArray[z].Font = font;
                buttonArray[z].Size = new System.Drawing.Size(100, 100);
                buttonArray[z].Location = new System.Drawing.Point(100*y, 100*x);
                buttonArray[z].Click += new EventHandler(button_click);
                z++; 
            }
        }//At the end of this loop, buttonArray contains a number of buttons equal to Dimensions all of which have a .Text property of ""
    }

So here is when the Form loads:

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            //Read the App.Config file to get the dimensions of the grid
            int dimensions = Convert.ToInt16(ConfigurationManager.AppSettings["dimensions"]);

            //Create the gameboard object with all the buttons needed
            Gameboard gb = new Gameboard(dimensions); //Calls the Gameboad constructor method

            //Add buttons to the form
            for (int x = 0; x < gb.buttonArray.Length; x++)
            {
                this.Controls.Add(gb.buttonArray[x]);
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

After I clear the buttons off the Form, what do I need to call to re-run the Form_Load so that it re-populates??

Upvotes: 0

Related Questions