|
-
Oct 15th, 2009, 04:09 AM
#1
Thread Starter
Hyperactive Member
[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??
-
Oct 15th, 2009, 11:42 AM
#2
Addicted Member
Re: Remove Items Read from file, from ListBox
Last edited by ZenDisaster; Oct 16th, 2009 at 08:52 AM.
-
Oct 16th, 2009, 12:38 AM
#3
Thread Starter
Hyperactive Member
Re: Remove Items Read from file, from ListBox
Never thought of using Generics 
Thanx sir, let me see if i can fix this issue once and for all
-
Oct 16th, 2009, 12:44 AM
#4
Thread Starter
Hyperactive Member
Re: Remove Items Read from file, from ListBox
Wait, Generics cannot be used with VS 2003. I think.
-
Oct 16th, 2009, 12:56 AM
#5
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.
-
Oct 16th, 2009, 01:09 AM
#6
Thread Starter
Hyperactive Member
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
-
Oct 16th, 2009, 01:20 AM
#7
Re: Remove Items Read from file, from ListBox
 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?
-
Oct 16th, 2009, 01:23 AM
#8
Thread Starter
Hyperactive Member
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|