【问题标题】:2 Dynamic Buttons, 1 Event Handler. How to determine who clicked the event?2 个动态按钮,1 个事件处理程序。如何确定谁点击了事件?
【发布时间】:2017-11-30 06:50:50
【问题描述】:

我正在尝试从我的 2 个动态按钮中确定谁单击了事件处理程序。

我如何在表单加载时创建我的 2 个动态按钮

private void Form1_Load(object sender, EventArgs e)
    {     
        for (int q = 0; q < 2; q++)
        {
            Point newLoc = new Point(a, b);              
            for (int i = 0; i <= 3 - 1; i++)
            {
                buttonArray[i] = new Button();
                buttonArray[i].Size = new Size(95, 80);
                buttonArray[i].Name = "btn" + q;
                buttonArray[i].Text = "btn" + q;
                buttonArray[i].Click += newButton;
                buttonArray[i].Location = newLoc;
                a = a + 10;
                if (a > 300)
                {
                    b = b + 100;
                    a = 1;
                }
                this.Controls.Add(buttonArray[i]);
            }
        }                 
    }

我试图调用的事件

   void newButton(object sender, EventArgs e)
    {
        if (sender == "btn1")
        {
            MessageBox.Show("btn1");          
        }

        if (sender == "btn2")
        {
            MessageBox.Show("btn2");
        }
    }

如果我不添加 IF 语句,它可以调用事件处理程序。

【问题讨论】:

    标签: c# winforms button dynamic


    【解决方案1】:

    您需要将object sender 转换为Button,然后针对Name 属性进行测试:

    void newButton(object sender, EventArgs e)
    {
        Button btn = sender as Button;
    
        // btn could be null if the event handler would be 
        // also triggered by other controls e.g. a label
        if(btn != null) 
        {
            if (btn.Name == "btn1")
            {
                MessageBox.Show("btn1");          
            }
    
            if (btn.Name == "btn2")
            {
                MessageBox.Show("btn2");
            }
        }
    }
    

    在 C# 7.0 中,您可以简化该调用:

    void newButton(object sender, EventArgs e)
    {
        if(sender is Button btn) 
        {
            if (btn.Name == "btn1")
            {
                MessageBox.Show("btn1");          
            }
    
            if (btn.Name == "btn2")
            {
                MessageBox.Show("btn2");
            }
        }
    }
    

    【讨论】:

    • 另一个选项是if(btn == btn1)。对于switch/case,使用string 是个好主意,尽管我会使用switch(btn.Name) { case btn1.Name: ...(或nameof),因为字符串文字不受重构影响。
    • @Sinatr 问题是,没有btn1 参考
    【解决方案2】:

    您有 2 个问题。
    一个:

    buttonArray[i].Name = "btn" + q;
    buttonArray[i].Text = "btn" + q;
    

    您应该为每个按钮创建并传递新值:

    int temp = q;
    buttonArray[i].Name = "btn" + temp;
    buttonArray[i].Text = "btn" + temp;
    

    More details

    两个:

    if (sender == "btn1")
    {
        MessageBox.Show("btn1");          
    }
    

    您将Button 对象与string 进行比较。您要做的是首先检查您的sender 是否为Button,然后检查它的Name 属性。

    Button btn = sender as Button;
    if(btn != null)
    {
        if (btn.Name == "btn1")
        {
            MessageBox.Show("btn1");
        }
    }
    

    【讨论】:

    • One 是什么?我不明白。
    • 添加了指向另一个答案的链接,其中包含有关装箱值的更多详细信息。
    • 好的,我不知道这个问题。尽管如此,您的代码不会解决问题,因为您仍然附加q 而不是temp。正确的代码应该是 int temp = q; buttonArray[i].Name = "btn" + temp; buttonArray[i].Text = "btn" + temp;'那么
    • 感谢您的提示 :)
    • 我和@RomanoZumbé 在一起,你在哪里看到有 One 的闭包?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    相关资源
    最近更新 更多