如何在所有表单上创建一个方法(winform)? [英] How raising a method on all forms (winform) created?

查看:59
本文介绍了如何在所有表单上创建一个方法(winform)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在创建所有表单类的所有实例时启动一个方法。

我试过在Application上搜索事件失败(如FormCreated或其他东西)。



我找到的唯一方法是创建一个继承自Form对象的对象,写一个加载event,并替换所有表单类定义的所有继承。我不喜欢做的是继续我的所有表单定义。有办法吗?



谢谢

Hi,

I'd like to launch a method when all instances of all form classes are created.
I tried to unsuccessfully search for an event on Application (like FormCreated or something else).

The only way I found is to create an object inheriting from the Form object, write a load event, and replace all inherition of all form class definitions. What I don't like to do is to go on all of my form definitions. There is a way to do that?

Thank you

推荐答案

我做了一些测试,看起来您可以使用消息过滤器:

I did some testing with this, and it looks like you can use a message filter for this:
// in Program.cs:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        CreatedFormMessageFilter filter = new CreatedFormMessageFilter();
        // use the OpenFormMessageFilter.FormCreated event to see opening forms
        Application.AddMessageFilter(filter);

        Application.Run(new Form1());
    }
}
class CreatedFormMessageFilter : IMessageFilter
{
    EventHandler _formCreated;
    public event EventHandler FormCreated
    {
        add
        {
            _formCreated += value;
        }
        remove
        {
            _formCreated -= value;
        }
    }
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 799)
        {
            if (_formCreated != null)
            {
                _formCreated(this, EventArgs.Empty);
            }
        }
        return false;
    }
}



以上代码基于消息过滤器。正如名称所示,过滤器可用于过滤特定消息,但这意味着消息将通过此方法。我做了一些测试,看起来消息ID 799 是你需要的。


首先,要加注一些方法(方法并没有真正提出它们只是被调用),你必须有这样的方法。如果你需要创建这样的方法,可以在某个表单类中创建它,并使所有其他表单继承这个类。



现在,如果你有很多表单实例或未知数字其中,您可能需要迭代所有这些。此属性为您提供一些应用程序实例的所有开放形式:

https://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms %28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.formcollection%28v=vs .110%29.aspx [ ^ ]。



所以,只需迭代 FormCollection 使用 foreach 循环并调用所需的方法。如果这是您在基类中创建的方法,遗憾的是,您必须将每个表单实例类型转换为基类类型。



这就是全部。问题解决了。



-SA
First of all, to "raise" some method (methods are not really "raised" they are just called), you have to have such method. If you need to create such method, create it in some form class and make all other forms inherit this class.

Now, if you have many form instances or unknown number of them, you probably need to iterate all of them. This property gives you all open forms of some Application instance:
https://msdn.microsoft.com/en-us/library/system.windows.forms.application.openforms%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.formcollection%28v=vs.110%29.aspx[^].

So, just iterate FormCollection using foreach loop and call the method you want. If this is the method you created in a base class, unfortunately, you have to typecast each form instance to your base class type.

That's all. Problem solved.

—SA


这篇关于如何在所有表单上创建一个方法(winform)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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