Pressing a button from another Form in C # [duplicated]

0

How can I make pressing a button on one form1 press another button on another form2 and form2 closes ??

// cambio el boton a public
public void btnoperator_Click(object sender, EventArgs e)
    {

    }

// en el otro form preosiono el boton del form1 y cierro el form2
private void pictureBox1_Click(object sender, EventArgs e)
    {
        f1.btnoperator.PerformClick();
        this.Close();
    }

What happens is that the function f1.btnoperator.PerformClick(); does not work, does not click the button of the other form.

    
asked by use2105 23.01.2017 в 17:11
source

1 answer

1

Do your click to button method on public Form2 and call it from the click method of Form1, as follows:

Class Form1
{
    public void Form1()
    { }

    private void button_Click(sender, e)
    {
        var frm = new Form2();

        frm.Show();
        frm.button_Click(this, null);
        frm.Close();
    }
}

Class Form2
{
    public void Form2()
    { }

    public void button_Click(sender, e)
    {
        ///...Tu codigo...///
    }
}

Or from the designer of Form1 you could instantiate Form2 and subscribe to the event with the same method that you have in Form2, in that way practice the effect that the click on Form2 will be the same as the click on Form1, it does the same as the code that I put you up but a little more practical.

    
answered by 23.01.2017 в 18:33