如何在非托管类中将System事件添加到System :: windows :: Forms :: Timer

时间:2011-10-28 09:30:45

标签: .net visual-c++ event-handling timer c++-cli

我正试图像我在某个例子中找到的那样:

TimerID = gcnew System::Windows::Forms::Timer(); 
TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick);

System::Void Bridge::timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
{
    Bridge::DoUpdate();
}

但由于错误,无法创建事件处理程序:

  

错误C3364:'System :: EventHandler':委托的参数无效   构造函数;委托目标需要是指向成员的指针   功能

Bridge是非托管类。 所以我用这种方式声明TimerID:

gcroot<System::Windows::Forms::Timer ^> TimerID;

我在这里做错了什么?如何正确添加Tick事件?

1 个答案:

答案 0 :(得分:1)

您没有发布足够的代码来诊断错误。这编译:

public ref class Bridge : public System::Windows::Forms::Form
{
    Timer^ TimerID;
public:
    Bridge(void) {
        InitializeComponent();
        TimerID = gcnew System::Windows::Forms::Timer(); 
        TimerID->Tick += gcnew System::EventHandler(this, &Bridge::timer1_Tick);        
    }
private:
    void Bridge::timer1_Tick(System::Object^  sender, System::EventArgs^  e) {
    }
    void InitializeComponent(void) {
        // etc...
    }
};