这个问题类似于  不同的按钮,当鼠标点击任意一个按钮的时候,都会触发一个按钮按下去的事件,那么我想知道怎么采用不同的方法处理
参考代码:
   #region 作为事件键值的对象
        private static readonly object startEvent = new object();
   #region 事件的定义
    public event EventHandler StartProcess 
    {
        add { this.Events.AddHandler(startEvent, value); } //这里的startEvent为委托所对应事件的对象,是不是用想办法传个唯一的标识来标记不同的按钮,就可以触发不同的处理方法?        remove { this.Events.RemoveHandler(startEvent, value); }
    }
 #region 触发事件的方法
        protected void OnStartProcess(EventArgs e)
        {
            if (this.Events[startEvent] != null)
            {
                (this.Events[startEvent] as EventHandler)(this, e);
            }
        }  // 触发处理管线的方法
        public void Process()
        {
            Console.WriteLine("开始处理 ");
            this.OnStartProcess(EventArgs.Empty);
         }
      class Program
{
    static void Main(string[] args)
    {
        ProcessPipeline process = new ProcessPipeline();
        process.StartProcess += new EventHandler(process_StartProcess);
              process.Process();
    }
 static void process_StartProcess(object sender, EventArgs e)
    {
        Console.WriteLine("开始处理的事件处理...");
    }

解决方案 »

  1.   

    我们经常说“一个event其实就是一个delegate”。实际上如果要看源代码你会发现,一个event它是一个MulticastDelegate,而这个多播事件才是从Delegate继承的。当一个事件触发,这一条语句可以触发几十、上百个方法也不在话下。如果你想去分别处理每一个方法,你也可以使用 GetInvocationList() 方法从 event 中获得各个 Delegate 的列表,然后逐一去调用它们。
      

  2.   

    既然 this.Events[startEvent] as EventHandler)(this, e); 这句话能从列表中取出对应的key为startEvent的Delegate ,
    那么怎么在
    public event EventHandler StartProcess 
      {
      add { this.Events.AddHandler(startEvent, value); }

    这里面 将startEvent赋值为 标识不同按钮的key的值呢?
      

  3.   


    那是不同的value值,所有值全都加入一个以startEvent为key的链表中。不是什么“将startEvent赋值”。我想你还没有看懂这个add代码。
      

  4.   

    先判断,再订阅或退订事件名+=new 委托类(函数名);//订阅
    事件名-=new 委托类(函数名);//退订
    触发事件的过程就和使用函数一样,直接 函数名(参数1,参数2,...)
      

  5.   

    贴一下代码 ,感觉有点混乱using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace buttonTest
    {  //声明委托
         public delegate void ButtonClickEventHandler(Object sender, EventArgs e);
        public class ProcessPipeline : System.ComponentModel.Component
        {
          
            
            // 按钮点击事件的定义
            private event ButtonClickEventHandler ButtonClick;
           
             // 注册事件
            public void Register(ButtonClickEventHandler method)
            {
                ButtonClick = method;
            }        // 取消注册
            public void UnRegister(ButtonClickEventHandler method)
            {
                ButtonClick -= method;
            }
           // 触发事件的方法
            public void OnButtonClick(EventArgs e)
            {
                if (ButtonClick != null)
                {
                    ButtonClick(this, e);
                }
            }    }    //定义按钮类
        class Button
        {
            int ButtonId;
            public Button(int x)
            {
                ButtonId = x;
            }
            public void DoSomething(object sender, EventArgs e)
            {
                Console.WriteLine("第{0}个按钮的点击事件已经处理乐", ButtonId);
            }    }    class Program
        {
            static void Main(string[] args)
            {
                ProcessPipeline process = new ProcessPipeline();            Button button1 = new Button(1);
                Button button2 = new Button(2);
                ButtonClickEventHandler bt1Handler=new ButtonClickEventHandler(button1.DoSomething);
               ButtonClickEventHandler bt2Handler=new ButtonClickEventHandler(button2.DoSomething);
                          
               
           
                int inputButtonId=Convert.ToInt32(Console.ReadLine());
                if (inputButtonId==1)
                {
                    process.Register(button1.DoSomething);
                   
                }
                else
                {
                    process.Register(button2.DoSomething);
                 
                }            process.OnButtonClick(EventArgs.Empty);
                       }
        }
    }
      

  6.   

    代码不会贴,重新贴次:using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace buttonTest
    {  //声明委托
         public delegate void ButtonClickEventHandler(Object sender, EventArgs e);
        public class ProcessPipeline : System.ComponentModel.Component
        {
          
            
            // 按钮点击事件的定义
            private event ButtonClickEventHandler ButtonClick;
           
             // 注册事件
            public void Register(ButtonClickEventHandler method)
            {
                ButtonClick = method;
            }        // 取消注册
            public void UnRegister(ButtonClickEventHandler method)
            {
                ButtonClick -= method;
            }
           // 触发事件的方法
            public void OnButtonClick(EventArgs e)
            {
                if (ButtonClick != null)
                {
                    ButtonClick(this, e);
                }
            }    }    //定义按钮类
        class Button
        {
            int ButtonId;
            public Button(int x)
            {
                ButtonId = x;
            }
            public void DoSomething(object sender, EventArgs e)
            {
                Console.WriteLine("第{0}个按钮的点击事件已经处理乐", ButtonId);
            }    }    class Program
        {
            static void Main(string[] args)
            {
                ProcessPipeline process = new ProcessPipeline();            Button button1 = new Button(1);
                Button button2 = new Button(2);
                ButtonClickEventHandler bt1Handler=new ButtonClickEventHandler(button1.DoSomething);
               ButtonClickEventHandler bt2Handler=new ButtonClickEventHandler(button2.DoSomething);
                          
               
           
                int inputButtonId=Convert.ToInt32(Console.ReadLine());
                if (inputButtonId==1)
                {
                    process.Register(button1.DoSomething);
                   
                }
                else
                {
                    process.Register(button2.DoSomething);
                 
                }            process.OnButtonClick(EventArgs.Empty);
                       }
        }
    }