删除列表控件(MFC)中的重复项

时间:2015-07-28 10:42:55

标签: c++ mfc

所以,我一直试图阻止在列表控件中添加文本/项目副本(它由文件浏览器添加)。我正在开发一个新的dll注入器以满足我的自定义需求和那个&#39这是我面临的唯一问题,我一直试图解决,但它仍然不是最好的选择。

我一直试图做的事情:

CFileDialog FileDialog(TRUE, L"*.*",    NULL, OFN_HIDEREADONLY, L"Dynamic Link Library (*.dll)|*.dll||");

    if (FileDialog.DoModal() == IDOK)
    {
        CString DllName = FileDialog.GetFileName();
        DllPathes.push_back(FileDialog.GetPathName());
        LVFINDINFO tempFind;
        tempFind.psz = DllName;
        tempFind.flags = LVFI_STRING;

        if (DllBox.FindItem(&tempFind))
        {
            DllBox.InsertItem(0, DllName);
        }
    } 

1 个答案:

答案 0 :(得分:3)

假设您的DllBox变量是CListCtrl,那么我想知道您为什么不检查FindItem的返回值,因为您当前的表达式将始终评估为true,除非index是0。

  

返回值
  如果成功则为项目的索引,否则为-1。


if (DllBox.FindItem(&tempFind) == -1) //Not found !
{
    DllBox.InsertItem(0, DllName);
}

如果您还将新选择的路径存储在容器DllPathes中,为什么不在此容器中搜索,并阻止其添加?

CString csSelected = FileDialog.GetPathName();
std::find(DllPathes.begin(), DllPathes.end(), [&](const CString &c)
                                              {return csSelected.Compare(c);});

您还应考虑为变量指定一个以小写字母开头的名称。特别是对于MFC课程,你可能会很快感到困惑。 FileDialog可以是继承自CFileDialog的类,还是变量? 您甚至可以看到Stackoverflow完成的格式化

相关问题