How to execute method in another form

Asked

Viewed 31 times

1

I cannot execute a form 2 method through the form 1 button

I call the form but the method does not appear

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        //executar
        Form2.metodo();
    }
}

with the two open forms did not work

        Form2 f = new Form2();
        f.metodo();
  • Without Form2 code it is difficult. But I can guess two possibilities. 1. in Form2 declare metodo() as static. Eg: public static void metodo(){}. 2. establish the class Form2. Eg: 'private void button1_Click(Object Sender, Eventargs e) { Form2 f = new Form2(); f.metodo(); <- call the method from the instance. }`

  • Where the method is declared metodo? Ask the question, is it public? Because if not, you won’t be able to access it anyway.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.