I was wondering, is it possible to deselect the selected item in a listbox by clicking in the white space of the listbox? If so, what notification would this be so I can do more than just deselect the item? Thanks for any help.
I was wondering, is it possible to deselect the selected item in a listbox by clicking in the white space of the listbox? If so, what notification would this be so I can do more than just deselect the item? Thanks for any help.
No, if you click in the whitespace of a list box it selects the item next to the whitespace clicked. The only time nothing is selected in a list box is when nothing has been clicked.
I meant the white space below the last item in a list box, where all the items in the listbox DONT fill the entire height of the listbox.
No, clicking in the white space below just keeps what was previously selected selected.
And there's no way around that?
I don't think so...In wxwigets there is a function to deselect an item in a list box, but I couldn't find a match on MSDN using Win32...
hmmmListBox.SetSelected Method
Selects or clears the selection for the specified item in a ListBox.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
I know I can do this:
SendMessage(hListBox, LB_SETCURSEL, (WPARAM)-1, 0);
But I'm unsure as to where.
Subclass the listbox control, handle its WM_LBUTTONDOWN message and hit test the mouse y-coordinate against the number of listbox items (LB_GETCOUNT) times the height of an individual item(LB_GETITEMHEIGHT). If the mouse y-coordinate is beneath the last item, then it's in whitespace; use SendMessage to deselect any selected item (LB_SETCURSEL) and reset the focus rectangle to the first item in the listbox (LB_SETCARETINDEX). Make sure you forward the WM_LBUTTONDOWN message to the default, system handler once you've done your hit testing.
CProgramming FAQ
Caution: this person may be a carrier of the misinformation virus.
Another option is to use a listview control and handle the NM_CLICK notfication.
Thanks for your help. I'll try to subclass the listbox.
I've made my own way and I don't care if its the best way because it works for me:
Code:case LBN_SELCHANGE: { int x = SendMessage(hWndList, LB_GETCURSEL, 0, 0); if(x != LB_ERR) { RECT rc; GetWindowRect(hWndList, &rc); int y = SendMessage(hWndList, LB_GETCOUNT, 0, 0); int z = SendMessage(hWndList, LB_GETITEMHEIGHT, 0, 0); y = y * z + rc.top + 2; // 2 because its 2 pixels from top of listbox to the top of the first item POINT pt; GetCursorPos(&pt); if(pt.y > x) { // In white space code } else { // Clicked item code } } break; }