MFC的ClistCtrl删除选中多行项目

MFC的ClistCtrl控件添加了多行数据后,若要删除选中的多行数据,可以使用ClistCtrl的成员函数,在网上找了很多例子,发现都有问题,因为在删除ClistCtrl行的时候,删除行下面的行会上移,那么下一个要删除的行的索引会改变,导致删除的是删除行下两行位置的数据,删除不完全。

使用下面代码可完全删除选中行:

void CBatchConvert::OnDeleteFile()
{
	// TODO: 在此添加命令处理程序代码
	POSITION pos = m_ctrlBatchFile.GetFirstSelectedItemPosition();
	while(pos)
	{
		int nIndex = m_ctrlBatchFile.GetNextSelectedItem(pos);
		m_ctrlBatchFile.DeleteItem(nIndex);
		pos = m_ctrlBatchFile.GetFirstSelectedItemPosition(); //这步很重要,不然删除不完全
	}
}

Comments are closed.