简体   繁体   中英

how to call non-static method on form1 from form2

can sameone provide example code on how to call non-static method on form1 from form2.

form1

    public Form1()
    {
        InitializeComponent();
    }

    public void prikazi()
    {
        MessageBox.Show("ok");
    }

    private void openf2_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.Show();
    }

form2

    public Form2()
    {
        InitializeComponent();
    }

    private void callMethod_Click(object sender, EventArgs e)
    {
        // this don't work. If I change to public static void on form1 then it work's but I need non-static example
        Form1.prikazi(); 
    }

thanks

It doesn't matter if it's a form class, if you want to access a non-static method, there is no other alternative then to create an instance of the class.

But - It doesn't make sense.. so don't do it

Find other alternatives, like creating the method you need static in a common place, or consider adding this method (or a variation of it) to the form

You need to have an instance of the form to call the method.

There are a few ways that you can make this work

1) Pass an action through to the new form

    public Form2()
    {
        InitializeComponent();
    }


    public Action yourAction {get; set;}

    private void callMethod_Click(object sender, EventArgs e)
    {
        Action instance = yourAction;
        if(instance != null)
            instance();
    }

then in Form 1 you can say

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.yourAction = prikazi;
    frm.Show();
}

2) You can pass an instance of Form1 into Form 2

So in Form 2 you have:

    public Form1 ParentForm {get; set;}

    private void callMethod_Click(object sender, EventArgs e)
    {
        if (ParentForm != null)
            ParentForm.prikazi();
    }

And Form1 you assign a value to the ParentForm variable

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.ParentForm= this;
    frm.Show();
}

form1

public Form1()
{
    InitializeComponent();
}

public void prikazi()
{
    MessageBox.Show("ok");
}

private void openf2_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(this);
    frm.Show();
}

form2

private Form1 parentForm;

public Form2(Form1 parentForm)
{
    this.parentForm = parentForm;
    InitializeComponent();
}

private void callMethod_Click(object sender, EventArgs e)
{
    parentForm.prikazi(); 
}

But better learn to bundle reusable code in to a separate class, rather than passing around form instances.

  public partial class Form1 : Form { internal static Form1 ViewForm1; // make other form run Public void public form1() { InitializeComponent(); ViewForm1 = this; //Add this } public void DoSomething() { //Code... } } ...................... public partial class Form1 : Form { public form2() { InitializeComponent(); Form1.ViewForm1.ShowData(); // call public void from form1 } 

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