Solved

NM_SETFOCUS in ListCtrl

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 114
Expand macro to ask for filename column 8 35
substring method in java 1 79
noX challenge 17 76
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

705 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

13 Experts available now in Live!

Get 1:1 Help Now