0

I have three WinForms .. Form1 Form2 and Form3

// Form1 Button

 private void btF1_Click(object sender, EventArgs e)
    {
        new Form3(this).ShowDialog();
    }

// Form2 Button

 private void btF21_Click(object sender, EventArgs e)
    {
        new Form3(this).ShowDialog();
    }

// Form3

      public partial class AjoutDemandeur : Form
{      
    Form1 _owner;
    Form2 _owner2;

    public Form3(Form1 owner, Form2 owner2)
    {
        InitializeComponent();
        _owner = owner;
        _owner2 = owner2;
    }

private void button1_Click(object sender, EventArgs e)
    {
        _owner.methodForm1(); //call a method from Form1
    }

private void button2_Click(object sender, EventArgs e)
    {
        _owner2.methodForm2(); // call a method from Form2
    }

I want to call a method from Form1 and Form2 into the Form3 But the problem is in the two buttons btF1 and btF2 => there is no argument given that corresponds to the required formal parameter 'owner2' of 'Form3.Form3(Form1, Form2)' So any solutions !

Semah
  • 77
  • 12
  • Why don't you make them properties on Form3 that can be set from Form1 and Form2? – Kidiskidvogingogin Sep 15 '16 at 13:29
  • 3
    Form3 requires two parameters Form1 owner, Form2 owner2 and you are sending only one. Change form3 to require only one parameter, or change your calling methods to send two parameters - or make the second parameter of form3's constructor optional using the THIS keyword (google c-sharp optional parameters) – Shannon Holsinger Sep 15 '16 at 13:30
  • I would also ask the question "Is this a function that needs to run immediatly, or can it be run when the form closes". if it is the latter, you can use the `FormClosing` event handler – Takarii Sep 15 '16 at 13:36
  • @Takarii yes I want to use it when Form3 closes .. but I think I have to call the function from the Form1 OR Form2 then use it in FormClosing Event .. is it right ? – Semah Sep 15 '16 at 13:42
  • Trying to make sense of this in my own head. The function you want to call is in form 1, but form 2 opens form 3? – Takarii Sep 15 '16 at 13:45
  • You should not have to pass form references into other forms in order to do this. When you do a Form.ShowDialog(otherForm) the Form.Parent will be set to the calling form. Then you could do in Form3: if (Parent !=null && (Parent as Form2) != null) //Parent is Form2. – Marc Lyon Sep 15 '16 at 14:04
  • Take a look at this post which contains some good options for different situations: [Interaction between Forms - How to Change a Control of a Form from Another Form?](http://stackoverflow.com/a/38769212/3110834) – Reza Aghaei Sep 15 '16 at 17:33

2 Answers2

1

Create events and their handlers in Form1 and Form2. Now fire those events from Form3.

Mukul Varshney
  • 3,131
  • 1
  • 12
  • 19
0

Solved ! Just I need to pass a null parameter in the calling methods

//Form1 Button

 private void btF1_Click(object sender, EventArgs e)
{
    new Form3(this,null).ShowDialog();
}

//Form2 Button

 private void btF21_Click(object sender, EventArgs e)
{
    new Form3(null,this).ShowDialog();
}
Semah
  • 77
  • 12