c# - 定义动态添加发件人的事件处理程序

标签 c# winforms

<分区>

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

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

这是我生成表单的代码

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

我没有问题,我觉得它看起来非常好 enter image description here

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

我该怎么做?

最佳答案

为按钮添加点击事件非常简单,像这样:

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

关于c# - 定义动态添加发件人的事件处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34682424/

相关文章:

c# - 更改任何一个元素时如何保持 RichText 格式(粗体/斜体/等)?

C# 在 List<T> Count 上等待

c# - 为我的应用程序重新分发和使用 CS.exe 编译器 (C#)

c# - 打开表单时如何设置对TextBox的关注?

c# - ASP.NET CORE 2 IdentityUser POCO 错误

c# - 在网络浏览器上显示自定义错误消息

c# - 以编程方式设置列表框从逗号分隔列表中选择的项目

C# Winform 数据网格增删改查

c# - 显示和隐藏用户控件 (BringToFront/SendToBack)

c# - Gtk# TreeView 将单个单元格设置为具有不同的渲染器?