Deselect all items in listbox-VBForums
Results 1 to 11 of 11

Thread: Deselect all items in listbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Deselect all items in listbox

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    List1.AddItem "Apple"
    List1.AddItem "Orange"
    List1.AddItem "Banana"
    List1.AddItem "Grape"
    End Sub
    
    Private Sub List1_Click()
    Text1.Text = List1.List(List1.ListIndex)
    End Sub
    Run that and whenever you click on an item, it is selected properly. But at the end of that operation, I want the item clicked to be deselected.

    On my VB, a selected item is colored blue. If I select a different item, the previously selected item turns white and the new item turns blue.

    I want to never see blue.

    Mac

    P.S. I hope you don't suggest
    Code:
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    List1.Clear
    List1.AddItem "Apple"
    List1.AddItem "Orange"
    List1.AddItem "Banana"
    List1.AddItem "Grape"
    End Sub
    Last edited by Mr.Mac; Aug 9th, 2007 at 12:18 PM.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    37,019

    Re: Deselect all items in listbox

    As .ListIndex specifies the selected item, change it to the appropriate value (-1 means no selection):
    Code:
    List1.ListIndex = -1

  3. #3
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: Deselect all items in listbox

    If you have a multiselect listbox, and want to unselect everything that has been selected, you can do
    Code:
    Dim i As Long
    For i = 0 To List1.ListCount - 1
        List1.Selected(i) = False
    Next
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2001
    Location
    Washington DC
    Posts
    314

    Re: Deselect all items in listbox

    Thanks, guys. Good stuff. But meanwhile I did this, which isn't too bad.

    Mac

    Code:
    Option Explicit
    Dim IgnoreThis As Boolean
    
    Private Sub Form_Activate()
    List1.ListIndex = 0
    End Sub
    
    Private Sub Form_Load()
    List1.AddItem "Select item below"
    List1.AddItem "Apple"
    List1.AddItem "Orange"
    List1.AddItem "Banana"
    List1.AddItem "Grape"
    End Sub
    
    Private Sub List1_Click()
    If IgnoreThis Then Exit Sub
    If List1.ListIndex > 0 Then
      Text1.Text = List1.List(List1.ListIndex)
    End If
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    IgnoreThis = True
    List1.ListIndex = 0
    IgnoreThis = False
    End Sub

  5. #5
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: Deselect all items in listbox

    or to beat the equine a little, more the following will invert the selections:

    VB Code:
    1. Dim i As Long
    2. For i = 0 To List1.ListCount - 1
    3.     List1.Selected(i) = Not List1.Selected(i)
    4. Next
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  6. #6
    Interweb adm/o/distrator Paul M's Avatar
    Join Date
    Nov 2006
    Location
    Australia, Melbourne
    Posts
    2,306

    Re: Deselect all items in listbox

    Code:
    Dim i As Integer
    For i = List1.ListCount - 1 To 0 Step -1
        If List1.Selected(i) = True Then
            List1.Selected(i) = False
        End If
    Next i
    I came up with this

  7. #7
    Lively Member
    Join Date
    May 2008
    Location
    Manila, Philippines
    Posts
    81

    Re: Deselect all items in listbox

    what if i want to remove the selected lists in my listbox, my multiselect = true
    this is error because if i removed list from my listbox, then the .ListCount is no longer the default value,

    what will i do then

    vbcode Code:
    1. Private Sub Command2_Click()
    2.    
    3.     Dim SelListCnt, listCount As Integer
    4.    
    5.     listCount = List2.listCount - 1
    6.     For SelListCnt = 1 To listCount
    7.         If List2.Selected(SelListCnt) Then
    8.             List2.RemoveItem SelListCnt
    9.            
    10.         End If
    11.     Next SelListCnt
    12.  
    13. End Sub

  8. #8
    Lively Member
    Join Date
    May 2008
    Location
    Manila, Philippines
    Posts
    81

    Re: Deselect all items in listbox

    sorry for the reply, i got the code from the other threads... sorry

  9. #9
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: Deselect all items in listbox

    I use this in one of my apps to deselect all items in a listbox.

    Code:
    Option Explicit
    Private Declare Function SendMessage Lib "user32" _
        Alias "SendMessageA" _
       (ByVal hwnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long
    
    Private Const LB_SETSEL = &H185&
    Private Sub Command1_Click()
    Call SendMessage(List1.hwnd, LB_SETSEL, False, ByVal -1)
    End Sub

  10. #10
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Deselect all items in listbox

    Quote Originally Posted by Mr.Mac
    Thanks, guys. Good stuff. But meanwhile I did this, which isn't too bad.

    Mac

    Code:
    Option Explicit
    Dim IgnoreThis As Boolean
    
    Private Sub Form_Activate()
    List1.ListIndex = 0
    End Sub
    
    Private Sub Form_Load()
    List1.AddItem "Select item below"
    List1.AddItem "Apple"
    List1.AddItem "Orange"
    List1.AddItem "Banana"
    List1.AddItem "Grape"
    End Sub
    
    Private Sub List1_Click()
    If IgnoreThis Then Exit Sub
    If List1.ListIndex > 0 Then
      Text1.Text = List1.List(List1.ListIndex)
    End If
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    IgnoreThis = True
    List1.ListIndex = 0
    IgnoreThis = False
    End Sub
    or to simplify

    Code:
    Option Explicit
    
    Private Sub Form_Activate()
    List1.ListIndex = 0
    End Sub
    
    Private Sub Form_Load()
    List1.AddItem "Select item below"
    List1.AddItem "Apple"
    List1.AddItem "Orange"
    List1.AddItem "Banana"
    List1.AddItem "Grape"
    End Sub
    
    Private Sub List1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        Text1.Text = List1.List(List1.ListIndex)
        List1.ListIndex = 0
    End Sub

  11. #11
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,334

    Re: Deselect all items in listbox

    Quote Originally Posted by youquijano
    what if i want to remove the selected lists in my listbox,
    Try this
    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim intCount As Integer
    intCount = List1.ListCount
    List1.Visible = False
    For i = intCount - 1 To 0 Step -1
        If List1.Selected(i) = True Then
            List1.RemoveItem i
        End If
    Next
    List1.Visible = True
    End Sub
    If you don't make the listbox temporarily invisible, then the routine will run slow because each item which is deleted will cause the listbox to be refreshed. By making the listbox temporarily invisible, the routine runs fast and there is no 'flickering' which takes place.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.