Quick listbox question

This is a discussion on Quick listbox question within the Windows Programming forums, part of the Platform Specific Boards category; I was wondering, is it possible to deselect the selected item in a listbox by clicking in the white space ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    193

    Quick listbox question

    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.

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    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.

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    No, clicking in the white space below just keeps what was previously selected selected.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    And there's no way around that?

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    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...
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    ListBox.SetSelected Method
    Selects or clears the selection for the specified item in a ListBox.
    hmmm
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    I know I can do this:

    SendMessage(hListBox, LB_SETCURSEL, (WPARAM)-1, 0);

    But I'm unsure as to where.

  9. #9
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    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.

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Another option is to use a listview control and handle the NM_CLICK notfication.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    Thanks for your help. I'll try to subclass the listbox.

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    193
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question on listboxes
    By Eibro in forum Windows Programming
    Replies: 2
    Last Post: 08-19-2002, 12:20 AM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21