Toggling highlighting through a CListCtrl in the correct order
-
Hi guys. My MFC application uses a CListCtrl with the icon style. The List Ctrl handles messages that make it select/highlight the first, previous, next and last icon, for navigation purposes. However, the order that selection toggles is wrong (it's consistent though) - It is as if the CListCtrl has a different conception of their actual order. I can toggle through the icons 1st, 4th, 2nd, 3rd. I can press "First", send a message, and get to the 1st. I can press last, and get to the third. I can toggle back and forth in that consistent, incorrect order. What might I be doing wrong? Any suggestions as to how I might solve this problem are greatly appreciated. Here is the relevant code:
void CMyView::OnInitialUpdate()
{
.....
MyListCtrl.ModifyStyle(0, LVS_AUTOARRANGE);
MyListCtrl.SetImageList(&imgl, 0);
LVITEM lvi0;
lvi0.mask = LVIF_IMAGE | LVIF_TEXT;
lvi0.iSubItem = 0;
lvi0.pszText = "First Icon";
lvi0.iImage = 0;int first\_icon = MyListCtrl.InsertItem(&lvi0); MyListCtrl.SetItemData(first\_icon, 1); ....
}
LRESULT CMyView::OnPressedFirst(WPARAM, LPARAM lParam)
{
ClearSelection();
MyListCtrl.SetItemState(0, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
return 0;
}
LRESULT CMyView::OnPressedPrevious(WPARAM, LPARAM lParam)
{MyListCtrl.SetFocus(); int i = MyListCtrl.GetItemCount(); int j = 0; for(; j != i; ++j) { UINT uState = MyListCtrl.GetItemState(j, LVIS\_SELECTED | LVIS\_FOCUSED); if (uState & LVIS\_SELECTED) break; } if(j == i) { MyListCtrl.SetItemState(0,LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING, LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING); return 0; } --j; // it's the previous one if( j < 0) { MyListCtrl.SetItemState(0,LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING, LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING); return 0; } ClearSelection(); MyListCtrl.SetItemState(j,LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING, LVIS\_SELECTED | LVIS\_FOCUSED | LVIS\_ACTIVATING); return 0;
}
LRESULT CMyView::OnPressedNext(WPARAM, LPARAM lParam)
{
MyListCtrl.SetFocus();
int i = MyListCtrl.GetItemCount();
int j = 0;
for(; j != i; ++j)
{
UINT uState = MyListCtrl.GetItemState(j, LVIS_SELECTED | LVIS_FOCUSED);
if (uState & LVIS_SELECTED)
break;
}
if(j == i)
{
MyListCtrl.SetItemState(0,LVIS_SELECTED | LVIS_FOCUSED | LVIS_ACTIVATING, LVIS_SELECTED | LVIS_FOCUSED | LVIS_ACTIVATING);
return 0;
}
++j; // it's the next one
if( j >= i)
{
return 0;
}