dcsimg
Results 1 to 8 of 8

Thread: [RESOLVED] Remove Items Read from file, from ListBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [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??

  2. #2
    Addicted Member ZenDisaster's Avatar
    Join Date
    Dec 2006
    Location
    Bay Area, CA
    Posts
    140

    Re: Remove Items Read from file, from ListBox

    Just do what jmc said.
    Last edited by ZenDisaster; Oct 16th, 2009 at 08:52 AM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Remove Items Read from file, from ListBox

    Wait, Generics cannot be used with VS 2003. I think.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,670

    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    104,670

    Re: Remove Items Read from file, from ListBox

    Quote Originally Posted by GrimmReaper View Post
    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?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    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
  •  



Featured


Click Here to Expand Forum to Full Width