Semah
Semah

Reputation: 87

Calling 2 methods from 2 different forms

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 !

Upvotes: 0

Views: 72

Answers (2)

Semah
Semah

Reputation: 87

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

Upvotes: 0

Mukul Varshney
Mukul Varshney

Reputation: 3141

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

Upvotes: 1

Related Questions