Solved

NM_SETFOCUS in ListCtrl

Posted on 2000-05-13
3
1,223 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Strange date stored 22 106
windows 10 how make picture as desktop background 2 58
noX challenge 17 93
Can not remove SSL certificate on iPhone 6 - iOS10.2 12 300
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

864 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now