从另一个表单调用方法

从另一个表单调用方法

本文介绍了从另一个表单调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从另一种形式调用方法.我的尝试:

I try to call a method from another form. My try:

public partial class newLedPopUp : Form
{
    Form1 back = new Form1();
    back.output();
    Close();
}

public partial class Form1 : Form
{
    newLedPopUp popup = new newLedPopUp();

    public void output()
    {
        button3_Click(null, null);
    }
}

有人可以帮我吗?我真的找不到我的错误,我已经找了很长时间了.

Can somebody help me? I really can't find my error and I've been looking for a very long time.

推荐答案

您可能需要一个已经打开的表单实例,并从那里调用该方法,而不是创建新表单的实例.你可以试试:

Instead of creating an instance of a new Form, you probably need an instance of already opened form and call the method from there. You can try:

if (System.Windows.Forms.Application.OpenForms["yourForm"] != null)
    {
        (System.Windows.Forms.Application.OpenForms["yourForm"] as Form1).Output();
    }

另外,您可以通过将事件的代码放在单独的方法中然后调用该方法来替换在 Output 方法中调用 button3_Click(null,null)针对您的按钮点击事件或您的公共输出方法

plus you can replace calling the button3_Click(null,null) in your Output method, by placing the code of the event in a separate method and then calling that method against your button click event or your public output method

这篇关于从另一个表单调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:01