[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
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
Re: Removing a specific item from a listbox
Doogle, that's a class act (EDIT 2) and a great insight into the earlier solution. :thumb:
Skip the file before the list is built. ;)
Re: Removing a specific item from a listbox
Oh you guys are so awesome! Thank you so much.