形式之间进行沟通的最佳方式? [英] Best way to communicate between forms?

查看:140
本文介绍了形式之间进行沟通的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎从来没有使用过(高级或全部)图形界面,或者一个简单的形式与简单的控制......但这次我已经得到的东西多一点复杂,我没有与GUI多少经验。
我有一个主要形式(也可能在未来更多)从其他子形式开放(他们可能有自己的子表单),我不知道是什么,在你看来,最好的办法在它们之间沟通?

I almost never used (advanced, or at all) graphical interfaces, or one simple form with simple controls... but this time I've got something a little more complex, and I don't have much experience with GUI. I have one main form (and possibly more in the future) from which other sub-forms open (and they might have sub-forms of themselves) and I wonder what is, in your opinion, the best way to communicate between them?

我想传递的主要形式作为参数的子表单的构造函数,但它似乎是一个很好的方法不对,尤其是如果我要去需要其他的,层次分明,分之间的通信形式,何况我必须仔细检查输入,或做一些方法,但它似乎更喜欢函数式编程不是面向对象的编程...

I thought of passing the main form as a parameter to the constructors of the sub-forms, but it doesn't seem like a good way, especially if I'm going to need to communicate between other, distinct, sub-forms, not to mention I have to double check the input, or make a few methods, but it seems more like functional programming than object oriented programming...

也许我可以..:
- 创建的全局设置一个静态类(或Properties.Settings)。缺点:需要数据的每一次变化被复制到类,我寻找的东西多一点comfrotable和优雅。
- 使用访问来自Application.OpenForms控件的方式 - 通过固定的主要形式为参数的问题。缺点:不是很稳定。
-DO别的东西我都没有想到的。 :P建议吗?缺点:不知道它是什么呢。 (

Perhaps I can..: -Create a static class (or Properties.Settings) for global settings. Cons: every change of data is needed to be copied to the class, I'm looking for something a bit more comfrotable and elegant. -Use the ugly way of accessing the controls from Application.OpenForms - fixes the problem of passing the main form as parameter. Cons: not very stable. -Do something else I haven't thought of. :P suggestions? Cons: don't know what it is yet. :(

先谢谢了。

推荐答案

一个好办法是声明的形式,要启动通信的代表。你需要一个代表和一个回调函数:

A good way is to declare delegates in the form that want to start the communication. You need a delegate and a callback function:

public delegate void SetValueDelegate(string value);
public SetValueDelegate SetValueCallback;

另一种形式就可以连接到该委托。在那一刻,这两种形式都知道对方,但不是之后的那一刻:

Another form can then attach to this delegate. At that moment both forms have to know each other, but not after that moment:

firstForm.SetValueCallback += new SetValueDelegate(secondForm.SetValueFunction);

第二种形式有权宣布该委托定义相匹配功能:

The second form has to declare a function that matches the delegate definition:

public void SetValueFunction(string value)
{
    // do something
}

现在第一种形式可以使用委托使用第二种形式贴到委托其他所有形式或者类的功能(和:

Now the first form can use the delegate to use the function of the second form (and all other forms or classes that were attached to the delegate:

SetValueCallback(txtParam.Text);

编辑:作出了完整的例子

using System;

namespace DelegateTest
{
    public delegate void SetValueDelegate(string value);

    public class Class1
    {
        public SetValueDelegate SetValueCallBack;

        public void Test()
        {
            if(SetValueCallBack != null)
            {
                SetValueCallBack("Hello World!");
            }
        }
    }

    public class Class2
    {
        public void SetValueFunction(string value)
        {
            Console.WriteLine(value);
        }
    }

    public class Launcher
    {
        public static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            Class2 c2 = new Class2();
            c1.SetValueCallBack += new SetValueDelegate(c2.SetValueFunction);
            c1.Test();
        }
    }
}

这篇关于形式之间进行沟通的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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