如何从另一种形式调用函数 [英] How to call function from another form

查看:65
本文介绍了如何从另一种形式调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我有一个设置表单和一个主表单。我试图从设置表单中调用Main表单的MasterReset函数,但没有任何反应。

主表单的MasterReset函数如下所示:



In my project I have got a Settings form and a Main form. I'm trying to call the Main form's MasterReset function from the Setting form, but nothing happens.
The Main form's MasterReset function looks like this:

public void MasterReset()
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to perform master reset? All settings will be set to default.", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            string phonebook_path = path + "\\Phonebook\\Contacts.xml";
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(phonebook_path);
            XmlNode xNode = xDoc.SelectSingleNode("People");
            xNode.InnerXml = "";
            xDoc.Save(phonebook_path);
            listView1.Clear();
            people.Clear();
        }
        else if (dialogResult == DialogResult.No)
        {
            return;
        }
    }





我正在从设置表单中访问它,如下所示:





And I'm accessing it from the Settings form like this:

private void btn_MasterReset_Click(object sender, EventArgs e)
{
    Main f1 = new Main();
    f1.MasterReset();
}





为什么我没有看到任何结果?



Why I am not seeing any results?

推荐答案

这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


创建第二个表单的实例,如果方法是公共方法,则可以直接调用它。
Create an instance of the second form and if the method is a public one, you can call it directly.


尝试在调试模式下运行它。一步步。看看发生了什么。



从描述来看,你的设计可能有点不对劲。如果主窗体是应用程序的启动窗体,而设置窗体是偶尔从Main显示的一个对话框,那么在设置中再次实例化Main只是为了调用方法没有多大意义。
Try do run it in debug mode. Step-by-step. And see what is happening.

From description, your design might be a little faulty. If Main form is start-up form of your application and Setting form is some dialog occasionally shown from Main, it doesn't make much sense to instantiate Main again in Settings just to call a method.


这篇关于如何从另一种形式调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆