c# - 在单独的类中引用表单

标签 c# winforms

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

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

编辑: @谢尔盖

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 实例?我只是对这个过程感到困惑。

这是如何在 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 实例作为参数传递给 Gameboard 构造函数或方法调用。

即在表单代码中:

Gameboard gameboard = new Gameboard(this);

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

在游戏板中:

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

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

关于c# - 在单独的类中引用表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15316821/

相关文章:

c# - 带列表框的 ContentPresenter

c# - 在与 SQL Server 的 ODBC 连接中打开多个结果集

c# - 向 MailChimp 添加新订阅者返回错误请求

C# 属性命名事件

c# - 如何防止禁用的 ToolStripMenuItem 在鼠标悬停时显示边框?

c# - 将文本文件中的每一行放入数组 C#

c# - sqlite 无法打开数据库文件是加密的还是不是数据库?

c# - WCF 服务未反序列化枚举值

C#。相同的代码仅适用于 1 个标签...为什么?

c# - 如何在编辑时在 Windows 应用程序中显示基于组合框选择的控件?