Solved

NM_SETFOCUS in ListCtrl

Posted on 2000-05-13
3
1,236 Views
Last Modified: 2013-11-20
I use a ListCtrl in a dialog. I map the NM_SETFOCUS message like this:

void CMainDlg::OnSetfocusFilelist(NMHDR* pNMHDR, LRESULT* pResult)
{
      NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
      int i = pNMListView->iItem;
      *pResult = 0;
}

I think i is the item number that be set focus. But it can't work. I can't get the right number. why?

BTW, if I click a item to select it, can I still get the item's selected state after the control is not in the ListCtrl? I hope to click some items in the ListCtrl and press a button, then my program will know which items I have selected . what is the better way to implement this?

Thank you very much.
0
Comment
Question by:Zhu051200
3 Comments
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 2806500
Try this to get the item no. in NM_SETFOCUS
void CMainDlg::OnSetfocusFilelist(NMHDR* pNMHDR, LRESULT* pResult)
{
  DWORD pos = ::GetMessagePos();
  CPoint point((int)LOWORD(pos),(int)HIWORD(pos));
   m_ListCtrl.ScreenToClient(&point);

  int itemno = m_ListCtrl.HitTest(point);
   ///////////////////
   // if (itemno == -1)
   //  No item selected
}

m_ListCtrl is a variable of type CListCtrl for the List Control

For the selection of items to remain after the listctrl has lost focus use the style LVS_SHOWSELALWAYS (Select the Show Selection Always on the Styles tab of list control)
0
 
LVL 1

Accepted Solution

by:
ch_vasu earned 50 total points
ID: 2806515
Hi,
If u Map NM_SETFOCUS, the function will be called only first time u focus to CListCtrl. So u cannot get the selected item at all.

I suggest to use any one of the following methods.
There are two ways to do this.
I) Single selection
1. Declare a member variable m_nItem in that class.
2. Map OnClickList (NM_CLICK).
add the body

NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
      m_nItem = pNMListView->iItem;
3. On press of a button you will be knowing the item selected index (i.e.,m_nItem). Using m_nItem u can access selected item information. (GetItemText(m_nItem,0) ...)

II). Better approach
If u have multiple/Single selections.

CListCtrl* pListCtrl = (CListCtrl*) GetDlgItem(IDC_YOURLISTCONTROL);
ASSERT(pListCtrl != NULL);

POSITION pos = pList->GetFirstSelectedItemPosition();
if (pos == NULL)
   TRACE0("No items were selected!\n");
else
{
   while (pos)
   {
      int nItem = pList->GetNextSelectedItem(pos);
      TRACE1("Item %d was selected!\n", nItem);
      // you could do your own processing on nItem here
   }
}


0
 

Author Comment

by:Zhu051200
ID: 2806576
Great!

I use ch_vasu's second way. It worked perfectly. It looks like that though the ListCtrl loses it's focus, it still remembers  which items have been selected.

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Detect CR LF to each line 12 170
twoTwo  challenge 35 107
pre4 challenge 19 104
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 875
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question