Solved

NM_SETFOCUS in ListCtrl

Posted on 2000-05-13
3
1,262 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

739 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