简体   繁体   中英

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 !

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM