Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to get the selected row/column text from the listcontrol ?
When i selected any text on listcontrol that data i have to show on status bar but i have no idea top get the selected text details from listcontrol.
could you please help me here

What I have tried:

i tried with getitemtext(0,0)(for example) but its not showing anything
Posted
Updated 6-Apr-17 3:51am
v2
Comments
Member 13089825 6-Apr-17 9:38am    
thank you for your reply
sorry i didnt get you answer,
My requirements is VC++ along with MFC
could you please help here how can i get selected text from listview
CHill60 6-Apr-17 9:51am    

1 solution

This depends on the type of selection (single or multiple rows).

To get the numbers of selected rows use CListCtrl::GetSelectedCount() [^]. If the count is not zero call
C++
prevItemIndex = GetNextItem(prevItemIndex, LVNI_SELECTED);
count times (or until -1 is returned) where prevItemIndex is -1 initially.

Alternatively use GetFirstSelectedItemPosition() and GetFirstSelectedItemPosition().

If your report list is not set to full row selection, you have to check each column of the selected rows using GetItem() and checking there for the selection state:
LVITEM item;
item.iItem = prevItemIndex;
item.mask = LVIF_STATE;
item.stateMask = LVIS_SELECTED;
GetItem(&item);
if (item.state & LVIS_SELECTED)
{
    // column is selected
}
Pass the returned index(es) to GetItemText() to retrieve the text.


If you want to react when the selection is changed (e.g. to update the status bar), handle the LVN_ITEMCHANGED notification code (Windows)[^]:
// Example for CListView derived class
// Message map entry:
ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, OnLvnItemChanged)

void CMyListView::OnLvnItemChanged(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    // Selection state has changed when LVIF_STATE is set and
    //  LVIS_SELECTED state differs
    if ((pNMLV->uChanged & LVIF_STATE) && ((pNMLV->uOldState ^ pNMLV->uNewState) & LVIS_SELECTED))
    {
        // pNMLV->iItem is the item and pNMLV->iItem the column
        //  for which the selection has changed
    }
    *pResult = 0;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900