CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    59

    Question [2003] 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
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: [2003] Remove Items Read from file, from ListBox

    I would change :
    Code:
                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
    To :

    Code:
                For I = (ItemCount - 1) To 0 Step -1
                    For Each ExcludeLine In ExcludeLines
                        ItemCount -= 1
    
                        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"
                            Exit For
                        End If
                    Next
                Next I
    The trick is to loop backwards in the listbox

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

Featured