我有2个事件:
this.lblSettings.MouseEnter += new System.EventHandler(this.HookEvent);
this.lblSettings.MouseLeave += new System.EventHandler(this.HookEvent);
他们称之为方法:
private void HookEvent(object sender, EventArgs e)
{
try
{
string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
Label label;
if (sender is Label)
{
//Get the event. eg: MouseEnter, MouseLeave
label = (Label)sender;
label.BackColor = Color.FromArgb(30, 30, 30);
}
else if (sender is PictureBox)
{
PictureBox picBox;
picBox = (PictureBox)sender;
Control control = picBox.Parent;
if (control is Panel)
{
foreach (Control ctrl in control.Controls)
{
if (ctrl is Label)
{
label = (Label)ctrl;
label.BackColor = Color.FromArgb(30, 30, 30);
break;
}
}
}
}
}
catch (Exception ex)
{
throw;
}
}
在LABEL部分,我有评论:“//获取事件。例如:MouseEnter,Mouseleave”,
我想看看这是否是正在执行的MouseEnter或MouseLeave事件,因为我想为每个事件做不同的事情。
答案 0 :(得分:1)
不可能,因为这些事件不使用EventArgs
子类。
编辑:
你当然可以创建一个new StackTrace()
对象并检查框架方法名称以查看它是哪个事件:)