Search code examples
c#eventstypescustom-events

c# eventArgs form events


I have multiple form with the same design like:

In Form1{
Form2 = new Formtoopen();
Form2.Resize += new EventHandler(Form2_Resize);
Form2.FormClosing +=new FormClosingEventHandler(Form2_FormClosing);
}

and then the events:

In Form1{
protected virtual void Fly_Form2_Closing(object sender, FormClosingEventArgs e)
{
   if (e.CloseReason == CloseReason.UserClosing)
   {
      e.Cancel = true;
      Fly_Form2.Hide();
   }
}

protected virtual void Fly_Form2_Visiblechanged(object sender, EventArgs e)
{
   //some code
}
}

I would like to add the Form2 Type in the EventArgs that is empty from now. I think it would make my code simpler as I have multiple Form sharing the same code.

How could I do that? I thought about the event custom arguments way but i'm not sure with Type...

Could you help me?

Thanks


Solution

  • Here is something that works:

     In Form1{
    Form2 = new Formtoopen();
    Form3 = new Formdata();
    Form2.FormClosing +=new FormClosingEventHandler(Form_FormClosing);
    Form3.FormClosing += new FormClosingEventHandler(Form_FormClosing);
    }
    
     In Form1{
    protected virtual void Form_Closing(object sender, FormClosingEventArgs  e)
    {
    if (e.CloseReason == CloseReason.UserClosing)
    {
      e.Cancel = true;
      ((Form)sender).Hide();
    }
    }
    

    Retrieving the sender with cast Form from the object sender.

    Thanks a lot M.Passant!