为多个表单设置单个事件处理程序 [英] Set up single event handler for multiple forms

查看:130
本文介绍了为多个表单设置单个事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些表单,在其中我有一些基本相同的事件功能



我已经尝试实现一个共享类,并将Eventhandler链接到那个功能,但是当我给予功能必要的保护等级时,它抱怨它是非静态的,我也必须使它静止。



我是不是静态功能的粉丝,所以问:有更好的方法吗?



(如果上述不清楚:我想这样做: a href =https://stackoverflow.com/questions/4157851/set-up-single-event-handler-for-multiple-buttons-in-net>在.NET中为多个按钮设置单个事件处理程序?,但使用多种形式而不是多重控件)



编辑:根据更多信息的要求:



我对于代码重复是相当的OCD,而且我的程序在同一时间有多个表单是主动/隐藏的,显然我想要按下这个x来关闭整个程序:

  class Shared 
{
public static void FormClosed(object sender,FormClosedEventArgs e)
{
Application.Exit ();
}

public static void FormClosing(object sender,FormClosingEventArgs e)
{
if(MessageBox.Show(你确定要退出吗? 确认退出,MessageBoxButtons.YesNo,MessageBoxIcon.Question)== DialogResult.No){
e.Cancel = true;
}
}
}

非常简单的功能,我知道,但我不喜欢重复:P



public static的上述配置工作正常,但我只是想知道是否有更好的方式 (tm)

解决方案

如果你不想要一个静态类,你有两个简单的选项来满足大多数偏好: p>


  • singleton

  • 传递参​​数以形成ctor



单身人士:

  class EventMangler {

private static readonly _instance = new SomeHandler();

//虽然你不喜欢静态方法:(
static EventMangler Instance {
get {return _instance;}

public void SomeEventHandler对象发件人,EventArgs e){
//处理事件
}
}

//使用EventMangler.Instance
public MyForm(){
InitializeComponent();
button1.Click + = EventMangler.Instance.SomeEventHandler;
}

要将参数传递给Form的构造函数,您有更多的选择:(a)将引用传递给处理程序的对象,或者(b)传递对处理程序本身的引用,我更喜欢选项(b)处理程序,否则,如果父对象 - 例如 EventMangler - 有多个处理程序,请使用选项(a):

  //从EventMangler中删除单例实例方法
//在程序中实例化EventMangler并传递给Form ctors

//将单个处理程序引用传递为Action
public MyForm(Action < object,EventArgs> handler){
InitializeComponent();
button1.Click + = handler;
}


I have some forms, and in them i have some event functions which are basically identical

I have tried to implement a 'Shared' class and link the Eventhandler to that function, but when i give the function the necessary protection level, it complains about it's non-static-ness and i have to make it static also.

I'm not a fan of static functions, and so ask: Is there a better way to do it?

(In case the above is unclear: I want to do this: Set up single event handler for multiple buttons in .NET? but with multiple forms instead of multiple controls)

EDIT: as per request for more info:

I'm fairly OCD about code duplication, and my program has multiple forms active/hidden at the same time, and obviously i want to close the whole program when the 'x' is pressed so:

class Shared
{
    public static void FormClosed(object sender, FormClosedEventArgs e)
    {
        Application.Exit();
    }

    public static void FormClosing(object sender, FormClosingEventArgs e)
    {
        if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) {
            e.Cancel = true;
        }
    }
}

Very simple functions, i know, but i don't like duplication :P

The above 'configuration' of 'public static' works fine, but i just wondered if there was a 'better way' (tm)

解决方案

If you don't want a static class, you have 2 easy options to suit most preferences:

  • singleton
  • pass parameter to form ctor

For a singleton:

class EventMangler {

   private static readonly _instance = new SomeHandler ();

   // although you don't like static methods  :(
   static EventMangler Instance {
      get { return _instance; }

   public void SomeEventHandler (object sender, EventArgs e) {
      // handle event
   }
}

// use EventMangler.Instance
public MyForm () {
   InitializeComponent();
   button1.Click += EventMangler.Instance.SomeEventHandler;
}

To pass a parameter to the Form's constructor, you have more choices: (a) pass reference to the handler's object, or (b) pass a reference to the handler itself. I prefer option (b) for a single handler. Otherwise, if the parent object - e.g. EventMangler - has multiple handlers, use option (a):

// remove singleton Instance method from EventMangler
// instantiate EventMangler in Program and pass to Form ctors

// pass a single handler reference as Action
public MyForm (Action<object, EventArgs> handler) {
   InitializeComponent();
   button1.Click += handler;
}

这篇关于为多个表单设置单个事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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