I have a dialog with a button.
Somewhere in another page, I am launching the dialog and registering Load event and firing the button click using button.PerformClick
This works fine when I create instance of dialog every time where i am planning to call. but if I reuse the dialog, second time onwards the button click event is not firing
public partial class dlg : Form
{ // button1 is public
public dlg() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { MessageBox.Show("XXX"); }
}
-------------
dlg d = new dlg(); // option 1
void Test()
{
// dlg d = new dlg(); // option 2
d.Load += new EventHandler(delegate(object a, EventArgs ty) { d.button1.PerformClick(); });
d.ShowDialog();
}
If I enable option 2, I would get message "XXX" each time i call the Test method. If I enable option 1, first time it popups the message and not for the consequent times.
kris sarmaPosted Sep 28, 2012, 6:36 AM
FroglegPosted Sep 28, 2012, 4:03 AM
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Test();
}
void Test()
{
Form2 f2 = new Form2();
f2.Load += new EventHandler(delegate(object a, EventArgs ty) { f2.button1.PerformClick(); });
f2.ShowDialog();
}
this works every time
kris sarmaPosted Sep 28, 2012, 3:37 AM
FroglegPosted Sep 28, 2012, 2:09 AM
private void button1_Click(object sender, EventArgs e)
{
doWhatever();
}
private void button2_Click(object sender, EventArgs e)
{
doWhatever();
}
public void doWhatever()
{
messageBox.Show("whatever");
}