如何从窗体中清除按钮控件

本文关键字:清除 按钮 控件 窗体 | 更新日期: 2023-09-27 18:01:48

我的表单是空白的,除了一个菜单条在顶部的新游戏和退出。当表单加载时,它会被一个按钮网格填充,这些按钮的尺寸是从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类。谢谢所有。

如何从窗体中清除按钮控件

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

调用它编辑构造函数到
public Gameboard(int numberofButtons, Form1 frm)

并传递this,然后实例化它。然后在构造函数

中调用frm.MyInits();

这就是我的Gameboard构造函数,我将向你展示Form如何调用它来填充底部的按钮。

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

这是表单加载时的情况:

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

在我清除按钮的形式,我需要调用什么来重新运行Form_Load,使它重新填充?

不使用this.Controls.Clear

你可以通过这个循环。控件,并测试它是否为Button,然后将其删除。

一个更好的方法是创建一个容器,面板之类的,在里面创建你的按钮,并使用

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