Results 1 to 4 of 4

Thread: [RESOLVED] Removing a specific item from a listbox

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Resolved [RESOLVED] Removing a specific item from a listbox

    Hi there, I have a list called storedfilenames, and it has a lot of items, but there are 2 items among many i do not want. 1 is called intro and the other is called instruction. Is there is a simple way to remove these specific items from the listbox? Thanks a lot

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Removing a specific item from a listbox

    It would be easier to not add them in the first place especially if it's a long list. Also, it can be quite 'confusing' for the end user if they suddenly see things vanishing from a list!

    To remove items:
    Code:
    Dim intI As Integer
    For intI = storedfilenames.ListCount - 1 To 0 Step -1
        If storedfilenames.List(intI) = "intro" Or _
           storedfilenames.List(intI) = "instructions" Then storedfilenames.RemoveItem intI
    Next intI
    EDIT: There's a more long winded way which may be faster if the list is very big, using the SendMessage API
    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_FINDSTRINGEXACT = &H1A2
    Private Const LB_DELETESTRING = &H182
    Private Const LB_ERR = (-1)
    
    Private Sub Remove(lst As ListBox, strRemove As String)
    Dim lngReturn As Long
    lngReturn = SendMessage(lst.hwnd, LB_FINDSTRINGEXACT, -1&, ByVal strRemove)
    If lngReturn <> LB_ERR Then
        lngReturn = SendMessage(lst.hwnd, LB_DELETESTRING, lngReturn, ByVal 0&)
    End If
    End Sub
    Call by:
    Code:
    Call Remove(storedfilenames,"intro")
    Call Remove(storedfilenames,"instructions")
    EDIT2:

    I assume this is associated with one of your earlier posts regarding populating the ListBox. If that is the case then you can modify the code you were given to inhibit adding "intro" and "instructions" into the ListBox:
    Code:
    Dim NameFile As String, SubDir As String
    SubDir = CurDir$ & "\*.exe*" ' Change CurDir to whatever directory is being searched
    NameFile = Dir$(SubDir)
    Do While NameFile <> vbNullString
        If InStr(NameFile, "intro") = 0 And InStr(NameFile, "instructions") = 0 Then
            List1.AddItem NameFile
        End If
        NameFile = Dir$
    Loop
    Last edited by Doogle; Nov 24th, 2010 at 03:03 AM.

  3. #3
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Removing a specific item from a listbox

    Doogle, that's a class act (EDIT 2) and a great insight into the earlier solution.

    Skip the file before the list is built.
    Last edited by Code Doc; Nov 24th, 2010 at 09:00 PM.
    Doctor Ed

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    May 2006
    Posts
    2,295

    Re: Removing a specific item from a listbox

    Oh you guys are so awesome! Thank you so much.

Posting Permissions

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



Click Here to Expand Forum to Full Width