kris sarma

kris sarma

  • NA
  • 5
  • 3.1k

PerformClick not working for second time

Sep 28 2012 1:41 AM
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.

Answers (4)