【问题标题】:Add Events To Dynamically added Buttons in Winform将事件添加到 Winform 中动态添加的按钮
【发布时间】:2018-09-17 07:19:53
【问题描述】:

我已经以窗口形式动态添加了按钮控件,现在我想为每个按钮控件添加不同的事件。 这是我从数据库中动态添加按钮的代码。

private void GetButtonDynamically()
    {
        SqlConnection conn = GetConnection();
        conn.Open();
        using (conn)
        {
            SqlCommand cmd = new SqlCommand("Select MenuName from tblMainMenu",conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {

                    Button mybutton = new Button();
                    mybutton.Location = new Point(x, y + 54);
                    y += 54;
                    mybutton.Height = 44;
                    mybutton.Width = 231;

                    mybutton.BackColor = Color.Gainsboro;
                    mybutton.ForeColor = Color.Black;

                    mybutton.Text = reader["MenuName"].ToString();
                    mybutton.Name = reader["MenuName"].ToString();
                    mybutton.Font = new Font("Georgia", 12);

                    Controls.Add(mybutton);
                    mybutton.Click+= new EventHandler(mybutton_Click);

            }
            conn.Close();
        }
    }

现在我面临的问题是它为每个动态创建的按钮生成相同的事件,我希望每个按钮都有不同的方法

这是点击事件

 private void mybutton_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button is Clicked");
    }

【问题讨论】:

  • mybutton_Click 方法的签名是什么?
  • 每个按钮的不同方法之间有什么区别?
  • mybutton.MouseHover += mybutton_Click 应该可以工作,无需将其包装在委托中
  • @ChetanRanpariya 不同的事件意味着例如我想要一个按钮来显示按钮被点击,而在另一个按钮上我想要显示按钮被悬停。
  • 您应该知道要为哪个按钮处理Click 事件以及要为哪个按钮处理Hover 事件。然后将这些事件处理程序附加到这些按钮。还要记住,事件处理程序的sender 参数包含其事件发生的按钮,您可以通过以下方式获取该按钮:var b = (Button)sender;

标签: c# sql-server winforms mouseevent mousehover


【解决方案1】:

您可以在创建按钮时为其添加“AccessibleName”。在您的情况下,在您的 while 循环中,并在按钮单击事件中获取可访问的名称,并应用 switch case 或循环来区分它。示例代码

int x = 10; int y = 10;
        for (int i = 1; i < 5; i++)
        {
            Button mybutton = new Button();
            mybutton.Location = new Point(x, y + 54);
            y += 54;
            mybutton.Height = 44;
            mybutton.Width = 231;
            mybutton.BackColor = Color.Gainsboro;
            mybutton.ForeColor = Color.Black;
            mybutton.Text = i + "MenuName".ToString();
            mybutton.Name = i + "MenuName".ToString();
            mybutton.AccessibleName = i.ToString();
            mybutton.Font = new Font("Georgia", 12);
            Controls.Add(mybutton);
            mybutton.Click += new EventHandler(mybutton_Click);
        }

在按钮中点击修改如下

        private void mybutton_Click(object sender, EventArgs e)
        {
          Button cb = (Button)sender;
          string strName = cb.AccessibleName;
          switch (strName)
          {
            case "1":
                MessageBox.Show("Button 1 is Clicked");
                break;
            case "2":
                MessageBox.Show("Button 2 is Clicked");
                break;
            case "3":
                MessageBox.Show("Button 3 is Clicked");
                break;
            default:
                break;
         }
       }

【讨论】:

    【解决方案2】:

    我认为它可以帮助你:

    private void Form1_Load(object sender, EventArgs e)
                {
                    int y = 0;
                    for (int i = 0; i < 10; i++)
                    {
    
                        Button mybutton = new Button();
                        mybutton.Location = new Point(0, y + 10);
                        y += 54;
                        mybutton.Height = 44;
                        mybutton.Width = 231;
    
                        mybutton.BackColor = Color.Gainsboro;
                        mybutton.ForeColor = Color.Black;
    
                        mybutton.Text = "a "+i.ToString();
                        mybutton.Name = "b" + i.ToString();
                        mybutton.Font = new Font("Georgia", 12);
    
                        switch (i)// define your condition
                        {
                            case 1:
                                mybutton.Click += new EventHandler(mybutton_Click);
                                break;
                            case 2:
                                mybutton.Click += new EventHandler(mybutton_1_Click);
                                break;
                            default:
                                break;
                        }
    
                        Controls.Add(mybutton);
                    }
                }
                private void mybutton_Click(object sender, EventArgs e)
                {
                    MessageBox.Show("Button 1 is Clicked");
                }
                private void mybutton_1_Click(object sender, EventArgs e)
                {
                    MessageBox.Show("Button 2 is Clicked");
                }
    

    【讨论】:

    • 对不起,我没有正确找到您的问题
    猜你喜欢
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多