c++ - Remove an item from a list-view control -
i have list-view control in program , want remove selected item. done button press.
the problem no matter item select deletes first one...
i think problem list-view focus being lost. when button pressed, list-view loses focus first, tries remove item that's not selected anymore, removes first one.
my question is: there option make list-view not lose focus?
edit:
here's code :
stdafx.h
// stdafx.h : include file standard system include files, // or project specific include files used frequently, // changed infrequently // #pragma once #include "targetver.h" #define win32_lean_and_mean // exclude rarely-used stuff windows headers // windows header files: #include <windows.h> #include <windowsx.h> #include <commctrl.h> // c runtime header files #include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> // todo: reference additional headers program requires here
main.cpp
#include "stdafx.h" lresult callback windowfunc(hwnd, uint, wparam, lparam); void createlist(); void createbuttons(); enum { idc_remove_button = 1000, idc_list }; struct listbox { hwnd hwnd; lvcolumn lvc; lvitem lv; listbox(hwnd h = 0, lvcolumn l = { 0 }, lvitem lvi = { 0 }) { hwnd = h; lvc = l; lv = lvi; } }; int selitm = -1; hinstance g_hinstance; hwnd hwndremovebutton; hwnd g_hwnd; listbox list; int winapi winmain(hinstance hthisinst, hinstance hprevinst, lpstr args, int winmode) { hwnd hwnd; msg message; wndclassex winclass = { 0 }; initcommoncontrolsex icc = { 0 }; g_hinstance = hthisinst; icc.dwsize = sizeof(icc); icc.dwicc = icc_listview_classes; initcommoncontrolsex(&icc); winclass.cbsize = sizeof(wndclassex); winclass.hinstance = hthisinst; winclass.lpszclassname = "test"; winclass.lpfnwndproc = windowfunc; winclass.style = 0; winclass.hicon = loadicon(null, idi_application); winclass.hiconsm = loadicon(null, idi_winlogo); winclass.hcursor = loadcursor(null, idc_arrow); winclass.lpszmenuname = null; winclass.cbclsextra = 0; winclass.cbwndextra = 0; winclass.hbrbackground = (hbrush)color_window; if (!registerclassex(&winclass)) return 0; hwnd = createwindow("test", "test", ws_overlappedwindow, cw_usedefault, cw_usedefault, 660, 350, null, null, hthisinst, null); showwindow(hwnd, winmode); updatewindow(hwnd); while (getmessage(&message, null, 0, 0)) { translatemessage(&message); dispatchmessage(&message); } return message.wparam; } lresult callback windowfunc(hwnd hwnd, uint message, wparam wparam, lparam lparam) { switch (message) { case wm_create: g_hwnd = hwnd; createlist(); createbuttons(); listview_insertitem(list.hwnd, &list.lv); listview_setcheckstate(list.hwnd, list.lv.iitem, true); listview_setitemtext(list.hwnd, list.lv.iitem, 1, "one"); listview_setitemtext(list.hwnd, list.lv.iitem, 2, "$1"); listview_setitemtext(list.hwnd, list.lv.iitem++, 3, "2010-05-05"); listview_insertitem(list.hwnd, &list.lv); listview_setcheckstate(list.hwnd, list.lv.iitem, true); listview_setitemtext(list.hwnd, list.lv.iitem, 1, "two"); listview_setitemtext(list.hwnd, list.lv.iitem, 2, "$2"); listview_setitemtext(list.hwnd, list.lv.iitem++, 3, "2008-05-05"); listview_insertitem(list.hwnd, &list.lv); listview_setcheckstate(list.hwnd, list.lv.iitem, false); listview_setitemtext(list.hwnd, list.lv.iitem, 1, "three"); listview_setitemtext(list.hwnd, list.lv.iitem, 2, "$3"); listview_setitemtext(list.hwnd, list.lv.iitem++, 3, "2006-05-05"); break; case wm_command: // low word of wparam contains menu id. switch (loword(wparam)) { case idc_remove_button: selitm = listview_getfocusedgroup(list.hwnd); listview_deleteitem(list.hwnd, selitm); break; } break; case wm_destroy: postquitmessage(0); break; default: return defwindowproc(hwnd, message, wparam, lparam); } return 0; } void createbuttons() { hwndremovebutton = createwindow("button", "remove", bs_pushbutton | ws_child | ws_visible, 20, 265, 70, 30, g_hwnd, (hmenu)idc_remove_button, g_hinstance, null); } void createlist() { list.hwnd = createwindow(wc_listview, null, ws_child | ws_visible | lvs_report, 20, 30, 600, 230, g_hwnd, (hmenu)idc_list, g_hinstance, null); listview_setextendedlistviewstyle(list.hwnd, lvs_ex_checkboxes | lvs_ex_fullrowselect | lvs_ex_headerdragdrop); list.lvc.mask = lvcf_text | lvcf_subitem | lvcf_width | lvcf_fmt; list.lvc.fmt = lvcfmt_left; /* add 4 columns list-view (first column contains check box). */ list.lvc.isubitem = 0; list.lvc.cx = 50; list.lvc.psztext = "good?"; listview_insertcolumn(list.hwnd, list.lvc.isubitem++, &list.lvc); list.lvc.cx = 300; list.lvc.psztext = "name"; listview_insertcolumn(list.hwnd, list.lvc.isubitem++, &list.lvc); list.lvc.cx = 150; list.lvc.psztext = "cost"; listview_insertcolumn(list.hwnd, list.lvc.isubitem++, &list.lvc); list.lvc.cx = 100; list.lvc.psztext = "watched since"; listview_insertcolumn(list.hwnd, list.lvc.isubitem++, &list.lvc); list.lv.iitem = 0; }
your problem has nothing focus. using wrong api determine selected item. using listview_getfocusedgroup()
when need use listview_getnextitem()
instead:
selitm = listview_getnextitem(list.hwnd, -1, lvni_selected); if (selitm != -1) listview_deleteitem(list.hwnd, selitm);
Comments
Post a Comment