Stop a Listbox from staying selected

Started by c.smith, July 14, 2008, 04:12:42 PM

Previous topic - Next topic

c.smith

Hi,

I have a form with a listbox, how do i stop the item in the box from staying highlighted (selected) when the listbox loses focus?

Thanks  :)

TechSupport

I think that your options may be as follows:

(1) Use the LBS_NOSEL style to prevent the ListBox from ever getting a selection :)

(2) Use OwnerDrawn to handle highlighting/unhighlighting (this would be pretty complicated unless you are familiar with handling a lot of lower level details of Windows controls.

(3) (Easiest solution), Handle the LBN_KILLFOCUS notification message for the ListBox and manually unselect any selection. See code below:


Function FORM1_LIST1_LBN_KILLFOCUS ( _
                                   ControlIndex  As Long,  _  ' index in Control Array
                                   hWndForm      As Dword, _  ' handle of Form
                                   hWndControl   As Dword, _  ' handle of Control
                                   idListBox     As Dword  _  ' identifier of listbox
                                   ) As Long
   
   ' As the ListBox loses focus, set the ListBox to have no selection
   FF_ListBox_SetCurSel hWndControl, -1
   
End Function


Hope this is what you are looking for.


c.smith

Hi,

This doesn't work:
FF_ListBox_SetCurSel hWndControl, -1

it sets the first item as selected.

Any ideas?

Thanks  :)

TechSupport

It does work - it has to work  ;)  You must have some other code that is screwing with your ListBox.

FF_ListBox_SetCurSel is a simple wrapper around the %LB_SETCURSEL message to the ListBox. If the index specified is -1 then the selection is removed. The following is from the API documents:
Quote
Specifies the zero-based index of the string that is selected. If the index parameter is -1, the list box is set to have no selection.

I have also attached a simple project (with an EXE) that shows the concept in action.

Please let me know how you make out with this.