如何触发动态按钮的按钮事件?

时间:2019-05-02 13:46:20

标签: c# button event-handling

我有一个扫雷游戏。有一个动态创建的按钮板。我想做的是打开整个面板(基本上是对每个按钮执行一次单击事件)。

我尝试使用PerformClick方法,但是没有用。首先,我将btn.Click事件用作btn.Click += new EventHandler(btn_Click),但是由于左/右键单击的使用,我最近将其更改为btn.MouseUp事件。即使当我使用btn.Click事件时,PerformClick也无法正常工作。


        public Form1()
        {
            InitializeComponent();

            GenerateMineSweeper();

            // my try of using PerfomClick
            for (int i = 0; i < this.Controls.Count; ++i)
            {
                if (this.Controls[i] is Button)
                {
                    Button btn = (Button)this.Controls[i];
                    btn.PerformClick();
                }
            }
        }

        private void GenerateMineSweeper()
        {
            // board is 15 x 25
            Point p = new Point(0, 0);
            for (int i = 1; i <= 15; ++i)
            {
                for (int j = 1; j <= 25; ++j)
                {
                    Button btn = new Button();

                    btn.Location = p;
                    btn.Size = new Size(25, 25);
                    btn.BackColor = Color.RoyalBlue;
                    btn.FlatStyle = FlatStyle.Popup;

                    // btn.Click += new EventHandler(btn_Click);
                    btn.MouseUp += (s, e) =>
                    {
                        switch (e.Button)
                        {
                            case MouseButtons.Left:
                                btn_LeftClick(s, e);
                                break;
                            case MouseButtons.Right:
                                btn_RightClick(s, e);
                                break;
                        }
                    };

                    this.Controls.Add(btn);

                    p.X += 25;
                }
                p.Y += 25;
                p.X = 0;
            }
        }

        private void btn_LeftClick(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            // code
        }

        private void btn_RightClick(object sender, EventArgs e)
        {
            // code
        }

P.S。 :因为我使用MouseUp事件,所以我显然希望使用LeftClick事件触发器,而不是RightClick。

编辑:我也尝试这样做,如下:

for (int i = 0; i < this.Controls.Count; ++i)
{
    if (this.Controls[i] is Button)
    {
        Button btn = (Button)this.Controls[i];
        btn.MouseUp += btn_LeftClick;
    }
}

但是我得到了StackOverflowException mscorlib.dll中发生了'System.StackOverflowException'类型的未处理异常。)

1 个答案:

答案 0 :(得分:1)

这是一些示例代码,应该为您尝试做的每件事提供一个思路

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        //Let's put 5 buttons on the form
        Button btn = new Button();
        btn.Location = new Point(10, i * 25);
        if (i == 2)
        {
            btn.Name = "Bomb";
        }
        else
        {
            btn.Name = "btn" + i;
        }
        btn.Text = "Closed";
        //Add a Click EventHandler
        btn.Click += new EventHandler(btn_Click);
        //Add a MouseUp MouseEventHandler
        btn.MouseUp += new MouseEventHandler(btn_MouseUp);
        //Add them to the form
        Controls.Add(btn);
    }
}

private void btn_MouseUp(object sender, MouseEventArgs e)
{
    Button b = sender as Button;
    //Show me which button was used when this event was triggered
    //MessageBox.Show(e.Button.ToString() + " Mouse button was used.");

    //Since the Right button on mouseup will not be considered a Click, we can tell it to PerformClick
    if (e.Button == MouseButtons.Right)
    {
        b.PerformClick();
    }
}
private void btn_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    if (btn.Name == "Bomb" && gameOver == false)
    {
        gameOver = true;
        btn.Text = "Boom";
        ExplodeAll(btn);
    }
    else
    {
        btn.Text = "Opened";
    }
}

bool gameOver = false;
private void ExplodeAll(Button sender)
{
    foreach (Button b in this.Controls.OfType<Button>())
    {
        if (b.Name.StartsWith("btn"))
        {
            b.PerformClick();
        }
    }
}

编辑:添加了一个ExplodeAll方法,当单击炸弹时,该方法将打开所有按钮。点击第三个按钮将模拟一个炸弹,其中的.PerformClick()