崩溃列表视图项目删除?

时间:2013-02-13 21:25:33

标签: winforms visual-c++ listview crash

我使用以下内容清除整个listView

void Form1::button2_Click(System::Object^  sender, System::EventArgs^  e)
{
    ActiveControl = tabControl1->SelectedTab;

    if (listView3->Items->Count == 0)
    {
        ::MessageBox(0, "Please select data.", "Failed to clear data.", MB_OK | MB_ICONERROR);
    }
    else
    {
        listView3->Items->Clear();
    }
}

然后我尝试使用以下内容清除所选项目......但它崩溃了......

void Form1::listView3_ItemCheck(System::Object^  sender, System::Windows::Forms::ItemCheckEventArgs^  e)
{
    listView3->Items[e->Index]->Remove();
}

我会更换我的删除功能,因此它不会崩溃?

编辑:这是我添加到listView3的方式......

    ListViewItem^ subitem = gcnew ListViewItem();

    subitem->SubItems->Add(textBox2->Text);

    listView3->Items->Add(subitem);

1 个答案:

答案 0 :(得分:0)

如果该索引处的元素是空引用,则可能必须使用不同的方法。

尝试使用项目集合的RemoveAt功能,如下所示:

if (e->Index >= 0 && e->Index < listView3->Items->Count)
{        
    listView3->Items->RemoveAt(e->Index);
}
相关问题