Unselect item from a listbox

When data is retrieved from my database and placed in a listbox, the first item is always selected. However, I do not want that behavior. How do you programatically unselect items in a listbox

I know how to do it using a webform listbox, but the windowsform listbox does not work the same way.

Thanks,

Bill.. 


Answer this question

Unselect item from a listbox

  • John_NewSys

    Well, if you're interested in a slimy hack, I have one that appears to work.  It involves subclassing the listbox:


    Public Class MyListBoxSubClasser
        Inherits NativeWindow

        Private Const LB_SETCURSEL As Integer = &H186

        Public Sub New(ByVal hWnd As IntPtr)
            MyBase.AssignHandle(hWnd)
        End Sub

        Public Sub Dispose()
            MyBase.ReleaseHandle()
        End Sub

        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = LB_SETCURSEL Then
                m.WParam = IntPtr.op_Explicit(-1)
            End If
            MyBase.WndProc(m)
        End Sub
    End Class


    Then, when you are assigning your datasource to the listbox:

    Dim ml As New MyListBoxSubClasser(ListBox1.Handle)


    Ensure, though, that you call dispose on the object above when you are done with it.  I played around with this for about 20 minutes and the only instance that the LB_SETCURSEL message seemed to be coming through was when the datasource was being set...  Programmatic or User initiated selection did not result in this message.

    Hope this helps, let me know if you have questions.

  • Elf

    found this interesting answer at th bottom of this thread over at GotDotNet

    <a href="http://www.gotdotnet.com/Community/MessageBoard/Thread.aspx id=92055">ComboBox SelectedIndex Problem  Help Please!!!</a>

  • Jean van Schalkwyk

    Clever, but no. It works until you select something. Then, setting the SelectedItem to nothing does exactly what the other techniques do -- it sets the selection back to the 0th item.
  • bpeikes

    You can also ClearSelected() -- this isn't really easy to find, though.  I'll put in a bug for the next version to try to get something a little more intuitive (maybe SelectedItems/SelectedIndices.Clear).
  • Craig J

    Well, you're right. Just tried it and it plain doesn't work. Either way. That's not good. I'm not seeing an easy solution. Love to find one! 


  • Honey Chris

    did you try...


    MyListBox.SelectedItem = Nothing

  • Philip Coyner

    I even tried the ClearSelected() method and still did not work........

    I don't know why it's so hard to programaticlly unselect items from a listbox. Clearly, all the methods I've tried do not clear the selected items. I'm wondering if there is a bug with this functionality in .Net............... 

    fyi, I have a dataset bound to the listbox with a valuemember & display member column.


    Bill...........

  • UberNewbie

    Well, yes, interesting hacks. Should it be this difficult  

    BTW, your solution (and my original one, along with the ClearSelection method--that is, all the techniques) work if you're just filling in the list manually. It's when you bind to a data source that nothing works. 

  • Belzebuth

    Well, duh. I was thinking the VB6 way, and didnt' even make the effort to look for a "correct" method. I feel stoopid. <g>
  • FredLarson

    Set the SelectedIndex property to -1. That should do it! There may be a "cleaner" way (that is, less non-intuitive) but this works. 
  • Unselect item from a listbox