类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("***");
}
}
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("***");
}
}
...
else
...
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给出的都是一样的
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());
}
}