Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I restrict events firing more than once at a time

Since I don't have much reputation to post image, I am elaborating as a big question.

I have three windows forms in which the execution of events get increased each time the form is created.

1. Form1(MainForm)

Here I call the second form(SubForm) which contains a user control.

2. Form2(SubForm)

Here I call the second form(ChildForm) by clicking the user control.

3. Form3(ChildForm)

It contains an OK button.

Here is my problem.

  • First I open MainForm and click the button to open second form(SubForm).
  • Now in second form(SubForm) I click the user control which shows third form(ChildForm).
  • When I click OK button in the third form, the form gets closed.
  • Now I close the second form(SubForm) without closing first form(Main form) and click the button in the first form(MainForm) to open second form(SubForm) again.
  • Now I click user control in the second form(SubForm) and the third form(ChildForm) is opened.
  • Now, when I click OK in the third form(ChildForm), the event in the second Form(SubForm) is fired once again and third form(ChildForm) gets opened four times.
  • Now when I close the second form(SubForm) again and clicks the user control and take third form(ChildForm), the third form(ChildForm) gets opened three times etc etc.

Here is the code in first form(MainForm)

private void button1_Click(object sender, EventArgs e)
{
    SubForm obj = new SubForm ();     
    obj.ShowDialog();
} 

Here is the code in second form(SubForm) which contains user control

// Event generation
UserControl1.MouseUp += new EventHandler(this.Node_Click); 

// Event that calls the ChildForm 
private void Node_Click(object sender, EventArgs e)
{
       ChildForm obj = new ChildForm();     
       obj.ShowDialog();                
}

Here is the code in first form(MainForm)

private void btnOK_Click(object sender, EventArgs e)
{
   this.Close();
} 

Anyone knows why this is happening?

like image 810
Shilpa Praneesh Avatar asked Oct 19 '22 20:10

Shilpa Praneesh


1 Answers

Hey I am not really sure what's happening but if you want the node_click to be applied only once just declare a bool var canEnter=true; and do this

    private void Node_Click(object sender, EventArgs e) { 
    if(canEnter) {
           ChildForm obj = new ChildForm();     
           obj.ShowDialog();    
canEnter=false;
 } 
             }

and make event for the child form (closed event) and place in it canEnter=true; if I got you right I guess that's what you need to do :)

like image 59
Aodai Irshed Avatar answered Oct 31 '22 18:10

Aodai Irshed