Hi!

I've been having some problems in getting a hold of the current selected item. All the clicking works fine, but when using the up/down arrow key problems start occuring.

Lets say item 0 is marked when the user decides to unmark it. If the user then presses the down arrow key to get to item 1, the search will start at -1 and it will assume item 0 is selected instead of the real selected item. I'm not sure the best way to do this and have been struggling with this problem for two days now.

As I said, all the clicking parts work fine, but I'll paste the entire WM_NOTIFY message anyways. Just the LVN_KEYDOWN causing me problems right now.

case WM_NOTIFY:
		{
			switch(((LPNMHDR)lParam)->code)
			{
				case LVN_KEYDOWN:
				{
					LPNMLVKEYDOWN pnkd = (LPNMLVKEYDOWN)lParam;
					if (pnkd->wVKey == VK_DELETE)
						SendMessage(hwnd, WM_COMMAND, (WPARAM)ID_OFFERT_LISTVIEW_DEL, 0);
					else if (pnkd->wVKey == VK_UP)
					{
						int nRet = ListView_GetNextItem(offert_lv->GetWindow(), -1, LVNI_FOCUSED | LVNI_SELECTED);
						nLastItem = nRet - 1;
					}
					else if (pnkd->wVKey == VK_DOWN)
					{
						int nRet = ListView_GetNextItem(offert_lv->GetWindow(), -1, LVNI_FOCUSED | LVNI_SELECTED);
						nLastItem = nRet + 1;
					}
				}
				break;

				case NM_RCLICK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						//DoShowMenu(hwnd, Main_Menu);
					}
					else
					{
						nLastItem = -1;
						nLastSubItem = -1;
						//DoShowMenu(hwnd, Main_Menu2);
					}
				}
				break;

				case NM_CLICK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
					}
					else
					{
						nLastItem = -1;
						nLastSubItem = -1;
					}
				}
				break;

				case NM_DBLCLK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						//DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_DLG_EDIT_SERVER), hwnd, EditServerProc);
					}
					else
					{
						nLastItem = -1;
						nLastSubItem = -1;
					}
				}
				break;

				case NM_RDBLCLK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
					}
					else
					{
						nLastItem = -1;
						nLastSubItem = -1;
					}
				}
				break;

				default: break;
			}
		}
		break;

I guess it would be a possibility to never set nLastItem to -1 and instead keep a boolean or something to tell the program that the "selected" item can not be deleted as it is not even marked. But this sound a bit risky and surely there must be a better way?

The following seem to work, but I'll leave the thread open if anyone can suggest another way to solve this.

case WM_NOTIFY:
		{
			switch(((LPNMHDR)lParam)->code)
			{
				case LVN_KEYDOWN:
				{
					LPNMLVKEYDOWN pnkd = (LPNMLVKEYDOWN)lParam;
					if (pnkd->wVKey == VK_DELETE)
						SendMessage(hwnd, WM_COMMAND, (WPARAM)ID_OFFERT_LISTVIEW_DEL, 0);
					else if (pnkd->wVKey == VK_UP)
					{
						if (nLastItem > 0)
						{
							nLastItem -= 1;
							bCanDelete = true;
						}
					}
					else if (pnkd->wVKey == VK_DOWN)
					{
						signed int nlvItems = offerts.size();
						if (nLastItem < nlvItems-1)
						{
							nLastItem += 1;
							bCanDelete = true;
						}
					}
				}
				break;

				case NM_RCLICK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						//DoShowMenu(hwnd, Main_Menu);
						bCanDelete = true;
					}
					else
					{
						//DoShowMenu(hwnd, Main_Menu2);
						bCanDelete = false;
					}
				}
				break;

				case NM_CLICK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						bCanDelete = true;
					}
					else
						bCanDelete = false;
				}
				break;

				case NM_DBLCLK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						//DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(ID_DLG_EDIT_SERVER), hwnd, EditServerProc);
						bCanDelete = true;
					}
					else
						bCanDelete = false;
				}
				break;

				case NM_RDBLCLK:
				{
					LPNMITEMACTIVATE lpnmItem = (LPNMITEMACTIVATE)lParam;
					if (lpnmItem->iItem != -1)
					{
						nLastItem = lpnmItem->iItem;
						nLastSubItem = lpnmItem->iSubItem;
						bCanDelete = true;
					}
					else
						bCanDelete = false;
				}
				break;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.