0

I am making a music player program using MFC. I would like to add a function to select multiple values ​​in the listbox and delete them, but I would like to ask for advice. The option of the listbox is Extended, and multiple selection using the control keys is fine. Among the methods of listbox, it seems to be possible to use SetSel, GetCursel, GetSelItems, etc, but I can't solve it. I would appreciate it if you could give me a simple advice. Thank you very much.

void CMFC_MP3Dlg::DeleteList(CStatus* head)
{
    CFileFind finder;
    CString strTemp;
    CString strRemoveFile;
    POSITION pos;

    CStatus* pTemp = new CStatus();
    CStatus* pPrev = new CStatus();

    //int nSelCount = m_ListBox.GetCurSel();
    //m_ListBox.GetText(nSelCount, strTemp);

    LPINT lpSelItems = new int[m_nListBoxCount];

    m_ListBox.GetSelItems(m_nListBoxCount, lpSelItems);

    for (int nCount = 0; nCount <= m_nListBoxCount; nCount ++)
    {
        if(m_ListBox.SetSel(nCount,1))
        {
            m_ListBox.GetText(nCount, strTemp);
            m_vec.push_back(strTemp);
        }
    }

    std::vector<CString>::iterator iter;

    for(iter = m_vec.begin(); iter != m_vec.end();)
    {
        strRemoveFile.Format(_T("C:\\MFC_MP3\\%s"), *iter);

        BOOL bRet = finder.FindFile(strRemoveFile);

        if(bRet)
        {
            if(DeleteFile(strRemoveFile))
            {
                pPrev = NULL;
                pTemp = head;

                if(pTemp == NULL)
                {
                    return;
                }

                while(pTemp->m_strFileName != strTemp)
                {
                    pPrev = pTemp;
                    pTemp = pTemp->m_right;
                }

                if(pTemp->m_strFileName == strTemp)
                {
                    pPrev->m_right = pTemp->m_right;
                    pTemp->m_right->m_left = pPrev;
                    delete pTemp;
                }

                MessageBox(_T("삭제 성공!"));
                ShowList();
            }
            else
            {
                MessageBox(_T("삭제 실패!"));
            }
        }
        else
        {
            MessageBox(_T("File Not Found!"));
        }
        iter++;
    }
}

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • *"I can't solve it"* - Solve what? The documentation for [GetSelItems](https://learn.microsoft.com/en-us/cpp/mfc/reference/clistbox-class#getselitems) has sample code on how to properly use it. – IInspectable Mar 16 '21 at 10:58

1 Answers1

0

Does this help?

void CChristianLifeMinistryPersonalCopiesDlg::BuildSelectedArray()
{
    int         i, iSize, *pIndex;
    CString     strText;

    m_aryStrPublishers.RemoveAll();

    // get selected count
    iSize = m_lbPublishers.GetSelCount();
    if (iSize > 0)
    {
        pIndex = new int[iSize];
        m_lbPublishers.GetSelItems(iSize, pIndex);

        for (i = 0; i < iSize; i++)
        {
            m_lbPublishers.GetText(pIndex[i], strText);
            m_aryStrPublishers.Add(strText);
        }

        delete[] pIndex;
    }
}

This works fine for me.


Note that your code is incomplete though. We don't see you specify what the count is of the selected items. And we don't see you reset your vector array before you begin.

Have you actually debugged your code to see where it fails?

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    I did what you said and it worked fine. Your code has been very helpful. Thank you!! –  Mar 17 '21 at 02:43