【问题标题】:Defining an event handler where the sender was dynamically added [duplicate]定义动态添加发送者的事件处理程序[重复]
【发布时间】:2016-01-08 17:09:34
【问题描述】:

我正在尝试编写一个玩跳棋的 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;
        }
    }
}

我没有任何问题,在我看来它看起来很不错

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

我该怎么做?

【问题讨论】:

  • 您可以创建一个方法并将按钮与该方法挂钩。例如。 button.OnClicked += OnClicked
  • 您可以为该按钮或按钮 OnCleick 事件创建一个委托

标签: c# winforms


【解决方案1】:

为一个按钮添加点击事件很简单,像这样:

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

【讨论】:

  • 谢谢!非常感谢。
【解决方案2】:

您可以为按钮的Click 事件添加处理程序并使用sender 参数。

数组中按钮的索引也可能对您很重要,因此您可以将按钮的数组索引存储在Tag属性中并稍后使用。

在for循环中:

var button = PlayButtonArray[ButtonRowindex, ButtonColindex];
button.Tag= new Point(ButtonRowindex, ButtonColindex);
button.Click += 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));
}

【讨论】:

  • 它正在工作!谢谢!
  • 在 for 循环中,您正在创建按钮。在PlayButtonArray[ButtonRowindex, ButtonColindex] 之后或使用PlayButtonArray[ButtonRowindex, ButtonColindex] 代替var 按钮。
  • Button.Tag as Point 似乎不起作用。 point 是不可为空的值类型,必须与引用类型或可空值类型一起使用。
  • 只是一个存储(x,y)的数据结构。您不会将其用作位置。
  • @OriaGruber 当然可以。 Reza 是一位 WinForms 大师,你可以从他身上学到很多东西。根据您的最后评论,请使用var indexes = (Point)button.Tag;
猜你喜欢
  • 1970-01-01
  • 2011-04-25
  • 2010-12-04
  • 1970-01-01
  • 2014-07-06
  • 2016-06-06
  • 1970-01-01
  • 1970-01-01
  • 2020-03-06
相关资源
最近更新 更多