简体   繁体   English

如何从Form2调用Form1上的非静态方法

[英]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. 可以提供有关如何从form2调用form1上的非静态方法的示例代码。

form1 表格1

    public Form1()
    {
        InitializeComponent();
    }

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

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

form2 表格2

    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 1)将动作传递到新表格

    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 然后在表格1中,您可以说

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 2)您可以将Form1的实例传递给Form 2

So in Form 2 you have: 因此,在表格2中,您具有:

    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 然后您为Form1分配一个值给ParentForm变量

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

form1 表格1

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 表格2

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 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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