문제

If I check with the debugger inside the sender parameter, I can see my object with all its properties, but how can I access those properties? I have tried MyClass mc = MyClass as sender but it's null.

Here's my timer's tick event:

private void timerP_Tick(object sender, EventArgs e)
{
}

And here is the event that starts my timer:

void class_startTimerEvent(MyClass class)
{
    timerP.Tag = class;

    if (InvokeRequired)
        this.Invoke((MethodInvoker)delegate { timerP.Start(); });
    else
        timerP.Start();
}
도움이 되었습니까?

해결책

For a EventHandler that uses the standard implementation the sender parameter is always the object that raises the event, in your case it's the timerP object.

So you can get your MyClass object using

var timer = (Timer) sender;
var myClass = (MyClass) timer.Tag;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top