Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigating trough List View

Status
Not open for further replies.

ionutdinulescu

Programmer
Jun 11, 2002
59
GB
I have a CListCtrl control and a text box on a dialog. When I click on an item in the list, I display a corresponding message in the text box.
Now I would like to do the same when I press arrow and page down/up keys.

I'm currently using LVN_KEYDOWN message. The problem is that when I go from item no 0 to item no 1, the selected index change is not seen by the LVN_KEYDOWN (so in the message handler, the selected item will be 0 instead of 1). Another problem is that LVN_KEYDOWN message doesn't tell me what key I have pressed.

Do you guys know a workaround for this ?
 
ionutdinulescu,

You need to use LVN_ITEMCHANGED message. Goto classwizard and add a function for the CListCtrl for that message. Then in the function put the following:

void CTest1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

m_szEdit = m_lstList.GetItemText(pNMListView->iItem,pNMListView->iSubItem);

UpdateData(FALSE);

*pResult = 0;
}

Where CTest1Dlg is my dialog class and m_szEdit is the CString variable associated with my edit control on the dialog. m_lstList is the member variable associated with my CListCtrl.

 
Seems to work, but if I put a breakpoint in this function, then move from item 2 to item 0, it gets to the breakpoint 3 times. First 2 times pNMListView->iItem is still 2, and it gets to 0 the third time.

I can do a workaround for this, but it's a bit stupid that it happens. It doesn't happen in VB/Delphi/Java.
 
ionutdinulescu,

All you need to do is put a check in for:

pNMListView->uNewState != 0

as this value is 0 unless there is a state change only 1 of the 3 times the message handler gets called will be the one you are interested in.

So for example the code would now be:

void CTest1Dlg::OnItemchangedList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

if (pNMListView->uNewState != 0)
{
m_szEdit = m_lstList.GetItemText(pNMListView->iItem,pNMListView->iSubItem);

UpdateData(FALSE);
}

*pResult = 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top