在事件处理程序中解析Sender [英] Resolve Sender in event handler

查看:92
本文介绍了在事件处理程序中解析Sender的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友

i有一个类似

dear friends
i have a class like

class1{
int id;
string name;
public System.Windows.Forms.Timer TimerObj = new System.Windows.Forms.Timer();
}





然后创建对象





then create object

class1 Obj = new class1();

Obj.id=1;
Obj.name="AA";
TimerObj.Interval = 1000;
Obj.TimerObj.Tick += new EventHandler(Timer_Tick);





我希望在EventHandler中访问Obj





and i want to access Obj in EventHandler

private void Timer_Tick(object sender, EventArgs e)
{
// ??????  HOW I CAN ACCESS CLASS1 OBJ FROM SENDER
((Timer)sender).
}





谢谢



thanks

推荐答案

尝试:

Try:
    Obj.id=1;
    Obj.name="AA";
    TimerObj.Interval = 1000;
    TimerObj.Tag = Obj;
    Obj.TimerObj.Tick += new EventHandler(Timer_Tick);
    ...
private void Timer_Tick(object sender, EventArgs e)
    {
    Timer t = sender as Timer;
    if (t != null)
       {
       class1 c = t.Tag as class1;
       if (c != null)
          {
          ...
          }
       }
    }


这篇关于在事件处理程序中解析Sender的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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