[RESOLVED] Remove Items Read from file, from ListBox
Hello, me again...
OK, straight to the point.
I write folder names to a file like this :
Code:
Private Sub WriteExcludeFolders()
Dim SelFolders(clbExclude.CheckedItems.Count - 1) As String
Dim i As Integer
For i = 0 To clbExclude.CheckedItems.Count - 1
SelFolders(i) = clbExclude.CheckedItems(i)
Next i
If File.Exists(Application.StartupPath & "ExcludedFolders.txt") Then
File.Delete(Application.StartupPath & "ExcludedFolders.txt")
End If
srFile = New IO.StreamWriter("ExcludedFolders.txt")
For i = 0 To SelFolders.Length - 1
srFile.WriteLine(WallDir & "\" & SelFolders(i))
Next
srFile.Close()
End Sub
As you can see, it is folders I'd like to exclude from file searches.
Now, when my program launches, I do this :
Code:
Private Function ReadExcludeFolds(ByVal filePath As String) As String()
Dim sr As System.IO.StreamReader
Try
sr = New System.IO.StreamReader(filePath)
Return System.Text.RegularExpressions.Regex.Split(sr.ReadToEnd, "\r\n")
Finally
If Not sr Is Nothing Then sr.Close()
End Try
End Function
Private Sub LoadExcludeFolds()
Try
ExcludeLines = ReadExcludeFolds("ExcludedFolders.txt")
Dim ExcludeLine As String
Dim ItemsFound As String
Dim ItemCount As Integer = lstFound.Items.Count '- 1
Dim I As Integer
For I = 0 To ItemCount - 1
For Each ExcludeLine In ExcludeLines
ItemCount -= 1 ' lstFound.Items.Count - 1
If I >= ItemCount Then Exit For
ItemsFound = lstFound.Items(I).ToString().Substring(0, lstFound.Items(I).ToString().LastIndexOf("\"))
If ExcludeLine.Equals(ItemsFound) Then
lstFound.Items.RemoveAt(I)
NumFiles -= 1
lblNumFiles.Text = NumFiles.ToString() & " Items In Repository"
End If
Next
Next I
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
What is supposed to happen is:
My program launches, I call the LoadExcludeFolds sub. This should read the contents of the textfile, and if it finds a folder listed in it, in the listbox, it should remove that item.
This only removes a few items.
So, say I have this in my text file :
Code:
C:\Testing\Images\Cars
And i have this in my listbox
Code:
C:\Testing\Images\Cars\BMW.jpg
C:\Testing\Images\Cars\Mercedes.jpg
C:\Testing\Images\Cars\Audi.jpg
C:\Testing\Images\Cars\Mazda.jpg
It will only remove BMW and Mercedes.
Any advice, any other way of doing this??
Re: Remove Items Read from file, from ListBox
Re: Remove Items Read from file, from ListBox
Never thought of using Generics :blush:
Thanx sir, let me see if i can fix this issue once and for all :)
Re: Remove Items Read from file, from ListBox
Wait, Generics cannot be used with VS 2003. I think.
Re: Remove Items Read from file, from ListBox
If you want to remove items from a list in a For loop you have to count backwards. If you count forwards then every time you remove an item you will skip the next one because all the indexes are decremented by 1.
Re: Remove Items Read from file, from ListBox
So, you're saying I must do this :
Code:
For I = ItemCount - 1 to 0
I'm stupid, so maybe I don't understand you correctly :)
Re: Remove Items Read from file, from ListBox
Quote:
Originally Posted by
GrimmReaper
So, you're saying I must do this :
Code:
For I = ItemCount - 1 to 0
I'm stupid, so maybe I don't understand you correctly :)
What happened when you tested it?
Re: Remove Items Read from file, from ListBox
Yes! Yes! Yes! Yes! Yes! Hoot hoot!
I did this :
Code:
For I = (ItemCount - 1) To 0 Step -1
And it worked!!
Wow, such a small thing!
Thank you thank you thank you!
I wish I was as clever as you!