C ++ / CLR ListView-> ItemSelectionChanged事件未编译(委托)

时间:2017-06-01 00:26:28

标签: .net listview c++-cli clr

我正在尝试将ItemSelectionChanged事件附加到列表视图中,但我无法编译抱怨委托的代码:

error C2664: 'System::Windows::Forms::ListView::ItemSelectionChanged::add' : cannot convert parameter 1 from 'System::EventHandler ^' to 'System::Windows::Forms::ListViewItemSelectionChangedEventHandler ^'

我的代码:

MyDialog.cpp

lv->ItemSelectionChanged += gcnew System::EventHandler(this, &MyDialog::ItemSelectionChanged);

System::Void MyDialog::ItemSelectionChanged(System::Object^ sender, System::Windows::Forms::ListViewItemSelectionChangedEventArgs^ e)
{
 // Do whatever
}

MyDialog.h

ref class MyDialog
{
   public:
     // stuff
   private:
     System::Void ItemSelectionChanged(System::Object^ sender, System::Windows::Forms::ListViewItemSelectionChangedEventArgs^ e);

}

我还附加了其他事件(即ColumnClick)。

帮助表示赞赏。

1 个答案:

答案 0 :(得分:0)

使用this video计算编译错误通常更容易。这是MyDialog

的简单版本
ref class MyDialog sealed
{
    ListView^ lv = gcnew ListView();
    void ItemSelectionChanged(Object^ sender, ListViewItemSelectionChangedEventArgs^ e) { }

public:
    MyDialog() {
        lv->ItemSelectionChanged +=
            gcnew ListViewItemSelectionChangedEventHandler(this, &MyDialog::ItemSelectionChanged);
    }
};