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

Removing duplicates in List Control (MFC)

所以,我一直在努力防止在列表控件中添加 text/item 重复项(它是由文件浏览器添加的)。我正在为我的自定义需求开发一个新的 dll 注入器,这是唯一的我遇到的问题,我一直在努力解决,但它仍然不是最好的选择。

我一直在努力做的事情:

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);
        }
    } 

假设您的 DllBox 变量是 CListCtrl, then I wonder why you are not checking the return value of FindItem,因为您当前的表达式将始终计算为真,除非索引为 0。

Return Value:
The index of the item if successful or -1 otherwise.


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 classes,您很快就会感到困惑。 FileDialog 可以是继承自 CFileDialog 的 class 还是一个变量? 您甚至可以看到 Whosebug 完成的格式化 !