?
Solved

NM_SETFOCUS in ListCtrl

Posted on 2000-05-13
3
Medium Priority
?
1,280 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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

743 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