0

我的表格是空白的,除了顶部的新游戏和退出菜单条。当表单加载时,它会填充一个按钮网格,其尺寸是从 App.config 文件中读取的。清除新游戏表单上所有按钮的最佳方法是什么?

this.Controls.Clear();

以上不起作用,因为它也删除了表单顶部的菜单条,它只让我从表单中调用它。我还有一个在程序的 Gameboard 类中运行的函数

    public void ClearButtons()
    {

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

但是必须有更好的方法来做到这一点,如果我可以重新加载表单,它会用所需的按钮重新填充它,但我似乎无法弄清楚,尤其是从我的 Gameboard 类。谢谢大家。

4

4 回答 4

2

1)您可以使用以下代码代替清除所有按钮:

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

2) 如果您想从另一个类调用此代码,您必须将表单的实例传递给它,例如作为构造函数参数

3)创建public void MyInits()函数并从Form1_Load那里移动代码。然后只需调用它Form1_Load

4)编辑构造函数

public Gameboard(int numberofButtons, Form1 frm)

然后通过this然后实例化它。比frm.MyInits();在构造函数中调用

于 2013-03-02T20:14:38.653 回答
0

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??

于 2013-03-02T20:40:04.573 回答
0

而不是使用 this.Controls.Clear

您可以通过 this.Controls 循环,并测试它是否是一个 Button,然后将其删除。

不过,更好的方法是创建一个容器、面板或类似的东西,并在其中创建您的按钮并使用

this.Controls.Remove(myPanel);
myPanel.Dispose();
于 2013-03-02T20:11:04.513 回答
-1
int n = 0;
while (n < number)
{
    btnArray[n].Name = "" + (n + 1);
    tablePanel.Controls.Remove(btnArray[n]);
    n++;
}
于 2017-08-08T11:10:52.877 回答