类bt中有一个按钮,有一个事件,如何在此类的两个实例中调用这个事件,实现不同的功能?
class bt
{
    Public Button Btn_show=new Button();
    public void bt
    {
    Btn_show.Click += new System.EventHandler(Btn_show_Click);
    }
    private void button1_Click(object sender, EventArgs e)
    {
     MessageBox.show("***");
    }
}

解决方案 »

  1.   

    比如bt a=new bt();点实例a的按钮弹出一个消息框,bt b=new bt();点实例b.按钮执行另一个方法。如何实现?
      

  2.   

    在一个方法里调用 button1_Click(null,null)
      

  3.   

    if ((sender as Button).Text == "button1")
       ...
    else
       ...
      

  4.   

    不太明白,现在源码是这样
        class Btn    {
            public Form g = new Form();
            public Button a = new Button();
            public BindingSource bs =new BindingSource();
            public Btn()
            {
                g.Controls.Add(a);
                a.Text = "adfadsfasdf";
                a.Click+=new EventHandler(a_Click);
                g.Show();
                bs.DataSource = "asdfa";        }
            private void a_Click(object sender, EventArgs e)
            {
                
                MessageBox.Show(sender.ToString());        }
        }namespace WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Btn a = new Btn();
                
            }        private void button3_Click(object sender, EventArgs e)
            {
                Btn b = new Btn();
            }
                }
    }
    这个事件应该怎么区分是a调用的还是b调用的,现在sender给出的都是一样的
      

  5.   


    namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Btn a = new Btn("a");
            }        private void button3_Click(object sender, EventArgs e)
            {
                Btn b = new Btn("b");
            }    }
    }class Btn
    {
        public Form myForm = new Form();
        public Button myButton = new Button();
        private string name;    public Btn(string instanceName)
        {
            name = instanceName;        myButton.Text = "adfadsfasdf";
            myButton.Click += new EventHandler(myButton_Click);        myForm.Controls.Add(myButton);
            myForm.Show();
        }
        private void myButton_Click(object sender, EventArgs e)
        {
             
            MessageBox.Show(name+" "+sender.ToString());
        }
    }