Deleting an item from list box using API
-
Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.
-
Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.
-
Hello all, I have list box IDC_FILE_LIST and a add and remove (IDC_REMOVE) button. After adding some items in the list box, I want to remove an item which is selected by the user on clicking on remove button. For that I have used this code and its not working item selected by the user is not getting deleted can please tell me what is the error in the below code. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) { switch(Message) { case WM_COMMAND: switch(LOWORD(wParam)) { case IDC_REMOVE: { HWND hList = GetDlgItem(hwnd, IDC_FILE_LIST); int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam); } break; ----------- ---------------------- Tanking you, Suresh HC.
The problem arises in the code below:
Suresh H wrote:
int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam);
you are asking for the number of list items instead of the item currently selected. Moreover the
lParam
in theLB_DELETESTRING
should be zero. Try this:int curSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
SendMessage(hwnd, (UINT) LB_DELETESTRING, curSel, (LPARAM)0);hope that helps
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
The problem arises in the code below:
Suresh H wrote:
int count = SendMessage(hList, LB_GETCOUNT, 0, 0); SendMessage(hwnd,(UINT) LB_DELETESTRING,count,lParam);
you are asking for the number of list items instead of the item currently selected. Moreover the
lParam
in theLB_DELETESTRING
should be zero. Try this:int curSel = SendMessage(hList, LB_GETCURSEL, 0, 0);
SendMessage(hwnd, (UINT) LB_DELETESTRING, curSel, (LPARAM)0);hope that helps
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
Hi, To get selected item you should use LB_GETCURSEL (LB_GETCOUNT returns number of items). Besides if you want to use LB_DELETESTRING you need first retrieve what text is at selected item - LB_GETTEXT.
----------- Mila
-
Thank you very much sir the solution is working. int CurSel = SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_GETCURSEL, 0, 0); SendMessage(GetDlgItem(hwnd, IDC_FILE_LIST), LB_DELETESTRING, CurSel, 0);
-
you are welcome! :):-D:)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.