在单独的类中引用Form

本文关键字:引用 Form 单独 | 更新日期: 2023-09-27 18:03:20

我有一个Windows窗体项目在Visual Studio 2012 (c#)

我在我的项目中有一个名为Gameboard的类,我需要在该类中的方法中引用this。我如何在另一个类中引用表单?

编辑:@Sergey

public class Gameboard
{
    public Button[] buttonArray { get; set; }
    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 ""
    }

所以有很多其他的东西之后,在Gameboard类,但对于构造函数我也需要在一个表单实例传递?我只是对这个过程感到困惑。

下面是如何在Form_Load

上调用构造函数的
    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

Form实例作为参数传递给Gameboard构造函数或方法调用

。in Form code:

Gameboard gameboard = new Gameboard(this);

Gameboard gameboard = new Gameboard();
gameboard.MyMethod(this);

和Gameboard中的

public void MyMethod(Form mainForm)
{
    ... //Whatever
}

对于你的代码,它将是:
public class Gameboard
{
    ...
    public Gameboard(int numberofButtons, Form1 mainForm) //Constructor method that is referencing the App.config for the dimensions value to make the board
    {
        ... // init all buttons
        // for example
        for (int i = 0; i<buttonArray.Length; i++)
        {
            mainForm.Controls.Add(buttonArray[i]);
        }
    }
}

and

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, this); //Calls the Gameboad constructor method
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

通过传递表单的实例作为对GameBoard类的引用。例如,在你的应用程序的启动方法。

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var mainForm = new Form1();
        var gameboard = new GameBoard(mainForm);
        Application.Run(mainForm);
    }