多个按钮有不同的tag,在同一个事件中,我要确定当前点击的按钮是哪个,怎么办?

解决方案 »

  1.   

    using System;
    using System.Windows.Forms;class Form1 : Form
    {
      Form1()
      {
        Button btnA = new Button();
        btnA.Parent = this;
        btnA.Text   = "按钮&A";
        btnA.Tag    = "这是按钮A!";
        btnA.Click += new EventHandler(ButtonClick);    Button btnB = new Button();
        btnB.Parent = this;
        btnB.Text   = "按钮&B";
        btnB.Top    = 30;
        btnB.Tag    = "This is Button B";
        btnB.Click += new EventHandler(ButtonClick);
      }  void ButtonClick(object sender, EventArgs e)
      {
        // 这就是你要的:
        string tag = (sender as Control).Tag.ToString();
        MessageBox.Show(tag);
      }
      
      static void Main()
      {
        Application.Run(new Form1());
      }
    }