这个问题类似于 不同的按钮,当鼠标点击任意一个按钮的时候,都会触发一个按钮按下去的事件,那么我想知道怎么采用不同的方法处理
参考代码:
#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("开始处理的事件处理...");
}
参考代码:
#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("开始处理的事件处理...");
}
那么怎么在
public event EventHandler StartProcess
{
add { this.Events.AddHandler(startEvent, value); }
}
这里面 将startEvent赋值为 标识不同按钮的key的值呢?
那是不同的value值,所有值全都加入一个以startEvent为key的链表中。不是什么“将startEvent赋值”。我想你还没有看懂这个add代码。
事件名-=new 委托类(函数名);//退订
触发事件的过程就和使用函数一样,直接 函数名(参数1,参数2,...)
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);
}
}
}
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);
}
}
}