Get Index of Item Text in MFC CListCtrl

…衆ロ難τιáo~ 提交于 2019-12-06 12:20:46

问题


I've got a CString with a Text that also is an Item Text of my CListCtrl. For example:

CString m_SearchThisItemText = _T("Banana");

And in my CListCtrl

m_List.SetItemText (1, 1, _T ("Banana"));

Now I want to find out, on which Index the Text is.

CListCtrl::FindItem doesnt work. It only searches the name of the Item, not the Text.

I also tried this

for (Index= 0; dlg.GetSearchContentText () == m_List.GetItemText (Index, Spalte); Index++)// HIER IST NOCH EIN FEHLER.
{
    if (dlg.GetSearchContentText () == m_List.GetItemText(Index, Spalte))
    {
        m_List.SetItemState (Zeile, LVIS_SELECTED, LVIS_SELECTED); 
        m_List.SetFocus();
    }
}

But it doesnt work. It stops at Index 0

Can anyone help me, how to find out on which Item the text is.

I hope you understand my question.


回答1:


Iterate all the items and search in the column you want:

int nCol = 1;    // to search in the second column (like your question)
CString m_SearchThisItemText = _T("Banana");

for (int i = 0; i < m_List.GetItemCount(); ++i)
{
    CString szText = m_List.GetItemText(i, nCol);
    if (szText == m_SearchThisItemText)
    {
        // found it - do something
        break;
    }
}



回答2:


If you mean that you have a list view with several columns and you want to search in other columns than the first one, then FindItem won't help you. You'll have to explicitly write the find code yourself. You must iterate over all the rows in the list, and for each column of a row call GetItemText and compare what you get with the text you have.



来源:https://stackoverflow.com/questions/19789686/get-index-of-item-text-in-mfc-clistctrl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!