C# 定义动态添加发送方的事件处理程序

C# 定义动态添加发送方的事件处理程序,c#,winforms,C#,Winforms,我正试图编写一个玩跳棋的C#程序。到目前为止,我已经完成了所有“肤浅”的事情,如生成电路板和所有标签等 我的问题是所有控件都是动态添加的。所以我不能只是“双击”它们,然后定义在按钮点击事件中要做什么 这是我生成表单的代码 public partial class formGameBoard : Form { private readonly int m_BoardSize; private readonly string m_Player1Name

我正试图编写一个玩跳棋的C#程序。到目前为止,我已经完成了所有“肤浅”的事情,如生成电路板和所有标签等

我的问题是所有控件都是动态添加的。所以我不能只是“双击”它们,然后定义在按钮点击事件中要做什么

这是我生成表单的代码

    public partial class formGameBoard : Form
    {
        private readonly int m_BoardSize;
        private readonly string m_Player1Name;
        private readonly string m_Player2Name;
        private GameTile m_BlueTile;
        private bool m_IsThereBlue;

        public formGameBoard(int i_BoardSize, string i_Player1Name, string i_Player2Name)
        {
            m_BoardSize = i_BoardSize;
            m_Player1Name = i_Player1Name;
            m_Player2Name = i_Player2Name;
            if (m_Player2Name == "(Computer)")
            {
                m_Player2Name = "Computer";
            }
            m_IsThereBlue = false;
            InitializeComponent();

        }

        private void formGameBoard_Load(object sender, EventArgs e)
        {
            int SizeOfButton = 60;
            int ButtonRowindex = 0;
            int ButtonColindex = 0;
            this.Size = new System.Drawing.Size(30 + m_BoardSize * SizeOfButton, 100 + m_BoardSize * SizeOfButton);
            Button[,] PlayButtonArray = new Button[m_BoardSize, m_BoardSize];
            for (ButtonRowindex = 0; ButtonRowindex < m_BoardSize; ButtonRowindex++)
            {
                for (ButtonColindex = 0; ButtonColindex < m_BoardSize; ButtonColindex++)
                {
                    PlayButtonArray[ButtonRowindex, ButtonColindex] = new Button();
                    PlayButtonArray[ButtonRowindex, ButtonColindex].Size = new Size(SizeOfButton, SizeOfButton);
                    PlayButtonArray[ButtonRowindex, ButtonColindex].Left = 10 + ButtonRowindex * SizeOfButton;
                    PlayButtonArray[ButtonRowindex, ButtonColindex].Top = 50 + ButtonColindex * SizeOfButton;
                    if ((ButtonRowindex + ButtonColindex) % 2 == 0)
                    {
                        PlayButtonArray[ButtonRowindex, ButtonColindex].Enabled = false;
                        PlayButtonArray[ButtonRowindex, ButtonColindex].BackColor = Color.Gray;
                    }

                    this.Controls.Add(PlayButtonArray[ButtonRowindex, ButtonColindex]);
                }
            }

            FillButtons(PlayButtonArray);
        }

        public void FillButtons(Button[,] ButtonMatrix)
        {
            int i, j;
            for (i = 0; i < m_BoardSize; i++)
            {
                for (j = 0; j < m_BoardSize; j++)
                {
                    if ((i + j) % 2 == 1)
                    {
                        if (j <= (m_BoardSize / 2) - 2)
                        {
                            ButtonMatrix[i, j].Text = "O";
                        }

                        if (j > (m_BoardSize / 2))
                        {
                            ButtonMatrix[i, j].Text = "X";
                        }
                    }
                }
            }
        }

        struct GameTile
        {
            int RowIndex;
            int ColumnIndex;
        }
    }
}
公共部分类formGameBoard:表单
{
私有只读int m_BoardSize;
私有只读字符串m_Player1Name;
私有只读字符串m_Player2Name;
私人GameTile m_BlueTile;
私人布尔穆伊斯特兰;
公共表单游戏板(int i_BoardSize、字符串i_Player1名称、字符串i_Player2名称)
{
m_BoardSize=i_BoardSize;
m_Player1Name=i_Player1Name;
m_Player2Name=i_Player2Name;
如果(m_Player2Name==“(计算机)”)
{
m_Player2Name=“计算机”;
}
m_Istherebue=假;
初始化组件();
}
私有void formGameBoard_加载(对象发送方,事件参数e)
{
int SizeOfButton=60;
int ButtonRowindex=0;
int buttonclindex=0;
this.Size=新的系统.Drawing.Size(30+m_BoardSize*SizeOfButton,100+m_BoardSize*SizeOfButton);
按钮[,]PlayButtonArray=新按钮[m_BoardSize,m_BoardSize];
对于(ButtonRowindex=0;ButtonRowindex
我对它没有异议,在我看来它看起来很不错

我的问题是,所有这些按钮都是动态添加的。我没有拖拽它们。我在表单加载时创建了它们。现在我希望当我点击一个按钮时会发生一些事情。例如,我希望单击的按钮将其颜色更改为蓝色


如何才能做到这一点?

为按钮添加单击事件非常简单,如下所示:

buttonName.Click += (sender, e) => { buttonName.Foreground = Colors.Blue; };
或者,如果您想使其成为一种能够处理多次按钮单击的方法,您可以这样做:

buttonName.Click += YourButtonHandler;

private void YourButtonHandler(object sender, EventArgs e)
{
    // do something, for example:
    Button b = sender as Button;
    b.Foreground = Colors.Blue;
}

您可以将处理程序添加到按钮的
事件中,并使用
发送者
参数

另外,数组中按钮的索引可能对您很重要,因此您可以将按钮的数组索引存储在
标记中
属性中,稍后再使用它

在for循环中:

var button = PlayButtonArray[ButtonRowindex, ButtonColindex];
button.Tag= new Point(ButtonRowindex, ButtonColindex);
button.Click += Button_Click;
单击按钮的代码:

private void Button_Click(object sender, EventArgs e)
{
    var button = sender as Button;
    //You can manipulate button here

    //Also to extract the button index in array:
    var indexes = (Point)button.Tag;

    MessageBox.Show(string.Format("This is the button at {0}, {1}", indexes.X, indexes.Y));
}

您可以创建一个方法并用该方法连接按钮。例如,button.OnClicked+=OnClicked您可以为该按钮或按钮创建一个代理onclick事件它正在工作!非常感谢。在for循环中,您将在其中创建按钮。在
PlayButtonArray[ButtonRowindex,ButtonClindex]
之后,或者使用
PlayButtonArray[ButtonRowindex,ButtonClindex]
而不是var按钮。button.Tag as点似乎不起作用。点是不可为null的值类型,必须与引用类型或可为null的值类型一起使用。它只是一个要存储的数据结构(x,y)。您不能将其用作位置。@Origruber请确保它正常工作。雷扎是一位WinForms大师,你可以从他身上学到很多东西。根据您最后的评论,使用
var index=(Point)button.Tag谢谢!非常感谢。