0
votes

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 !

2
Why don't you make them properties on Form3 that can be set from Form1 and Form2? - Kidiskidvogingogin
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
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
@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
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

2 Answers

1
votes

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

0
votes

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();
}