简体   繁体   中英

Removing duplicates in List Control (MFC)

So, I've been trying to prevent adding text/item duplicates in List Control (It is added by File Browser).. I'm developing a new dll injector for my custom needs and that's the only problem I face and I've been trying to fix, but it is still not the best option.

What I've been trying to do:

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

Assuming your DllBox variable is the CListCtrl , then I wonder why you are not checking the return value of FindItem , as your current expression will always evaluate to true, except if the index is 0.

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


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

If you also store in the container DllPathes the newly selected path, why don't you search in this container too, and prevent it from being added ?

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

You should also consider giving your variables a name which starts with a lowercase letter. Especially with MFC classes you can get confused fast. Could FileDialog be a class which inherits from CFileDialog , or is it a variable ? You can even see the formatting done by Stackoverflow !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM