user7667690
user7667690

Reputation:

Make an Event When Second Form IsClosing

Let me say I have 2 forms

1st form name is : Form1

Consists from : 1 Button and it's name is Button1

This Button is Public from its properties and have an OnClick event

The event is:

        private void Button1_Click(object sender, EventArgs e)
    {
        Form2 form2 = new Form2();
        form2.Show();
        Button1.Enabled = false;
    }

2nd Form name is : Form2

Consists from : nothing

But there's a FormClosing event

The event is:

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        Form1 form1 = new Form1();
        form1.Button1.Enabled = true;
    }

Which means Button1 from Form1 should be Enabled again.

The problem nothing happens when the Form2 is Closed

I thought that event is incorrect but i'm not really sure

Any help is appreciated

Upvotes: 2

Views: 45

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

You need to pass the instance of Form1 to Form2, that way you have access to the object/form...

public class Form2
{
  private Form1 _Form1;
  public Form2(Form1 form1)
  {
     this._Form1 = form1;
  }

  private void Form2_FormClosing(object sender, FormClosingEventArgs e)
  {
      _Form1.Button1.Enabled = true;
  }    
}

...

public class Form1
{
  private void Button1_Click(object sender, EventArgs e)
  {
    Form2 form2 = new Form2(this); //pass "this", ie the instance of Form1
    form2.Show();
    Button1.Enabled = false;
   }
 }

Upvotes: 1

Related Questions