Results 1 to 36 of 36

Thread: [RESOLVED] help about listbox!!

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Resolved [RESOLVED] help about listbox!!

    hi guys, i am working on my search button that searches for the items in my listbox1. however, i have several problems.. one, is that i do not know how am i supposed to search in folder browsing dialog and two, i cannot search for the items inside my listbox1. any comments is very much appreciated. i am just a newbie and i think this is advanced for me, so please help me! thanks alot!

    this is my code:


    Code:
     For Each foundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories
    _MyDocuments, FileIO.SearchOption.SearchAllSubDirectories) --> Note 1
    
                If listbox1.Items.Contains(foundFile) Then  --> Note 2
    
                    listbox2.Items.Add(foundFile)  --> Note 3
                Else
    
                    listbox3.Items.Add(foundFile) --> Note 4
    
                End If
            Next

    note 1: i want to change the directory to a folder browse dialog not in my documents only
    note 2: this are the items in the listbox that i am searching for
    note 3: if found, the paths of the items will be displayed in this listbox
    note 4: if it is not found, it will be displayed here

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Using fbd As New FolderBrowserDialog
    5.             If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 For Each foundFile As String In My.Computer.FileSystem.GetFiles(fbd.SelectedPath, FileIO.SearchOption.SearchAllSubDirectories)
    7.                     If ListBox3.FindStringExact(foundFile) > -1 Then
    8.                         ListBox2.Items.Add(foundFile)
    9.                     Else
    10.                         ListBox3.Items.Add(foundFile)
    11.                     End If
    12.                 Next
    13.             End If
    14.         End Using
    15.  
    16.     End Sub
    17. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    hi paul, thanks for the reply..

    the file that im going to search is a .wav file.. that is why i code it like this,


    Code:
    If ListBox3.FindStringExact(foundFile + ".wav") > -1 Then
    
                            ListBox2.Items.Add(foundFile + ".wav") --> if found displays here
    
                        Else
    
                            ListBox3.Items.Add(foundFile + ".wav") --> if not, here
    
                        End If
    unfortunately, it still cannot search the items in listbox1, it displays all in the listbox3.
    Last edited by markirving; Sep 21st, 2010 at 04:43 PM.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    instead of:

    vb Code:
    1. My.Computer.FileSystem.GetFiles(fbd.SelectedPath, FileIO.SearchOption.SearchAllSubDirectories)

    use this:

    vb Code:
    1. IO.Directory.GetFiles(fbd.SelectedPath, "*.wav", IO.SearchOption.AllDirectories)

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    yes it only searches for .wav files.

    but my problem is this.. for example:

    1.the items in listbox1 are superman, batman and flash.
    2.now i only have superman.wav in my folder.
    3.when i click the search button, listbox2 displays superman.wav but it also displays batman.wav and flash.wav
    4.batman and flash should be displayed on listbox3 because i do not have batman.wav and flash.wav.

    can you help me solve this? im really stuck with this. sorry for giving u a hardtime. thanks dude.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Using fbd As New FolderBrowserDialog
    5.             If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 For Each foundFile As String In IO.Directory.GetFiles(fbd.SelectedPath, "*.wav", IO.SearchOption.AllDirectories)
    7.                     If ListBox1.FindStringExact(foundFile) > -1 Then
    8.                         ListBox2.Items.Add(foundFile)
    9.                     Else
    10.                         ListBox3.Items.Add(foundFile)
    11.                     End If
    12.                 Next
    13.             End If
    14.         End Using
    15.  
    16.     End Sub
    17. End Class

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    hey dude, i still have same results..

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    ok. i found the problem. listbox1 doesn't have file extensions, right?

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Using fbd As New FolderBrowserDialog
    5.             If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 For Each foundFile As String In IO.Directory.GetFiles(fbd.SelectedPath, "*.wav", IO.SearchOption.AllDirectories)
    7.                     If ListBox1.FindStringExact(IO.Path.GetFileNameWithoutExtension(foundFile)) > -1 Then
    8.                         'to add filenames without extensions
    9.                         'ListBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    10.                         ListBox2.Items.Add(foundFile)
    11.                     Else
    12.                         'ListBox3.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    13.                         ListBox3.Items.Add(foundFile)
    14.                     End If
    15.                 Next
    16.             End If
    17.         End Using
    18.  
    19.     End Sub
    20. End Class

  9. #9

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    wow! thanks dude! your the best!!

    one last thing though, what if for example what i am searching has a file name of batman_opening.wav but the item in listbox1 only has batman in it? is there a way for me to search all of the file names that has batman in it?

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Using fbd As New FolderBrowserDialog
    5.             If fbd.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 For Each foundFile As String In IO.Directory.GetFiles(fbd.SelectedPath, "*.wav", IO.SearchOption.AllDirectories)
    7.                     Dim contains = From item In ListBox1.Items _
    8.                                    Where foundFile.Contains(item.ToString) _
    9.                                    Select item
    10.                     If contains.Count > 0 Then
    11.                         'to add filenames without extensions
    12.                         'ListBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    13.                         ListBox2.Items.Add(foundFile)
    14.                     Else
    15.                         'ListBox3.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    16.                         ListBox3.Items.Add(foundFile)
    17.                     End If
    18.                 Next
    19.             End If
    20.         End Using
    21.  
    22.     End Sub
    23.  
    24. End Class

  11. #11

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    perfect! i have a question though, there is a warning in foundfile in this line..

    Code:
    Dim contains = From item In ListBox1.Items _
    
                                       Where foundFile.Contains(item.ToString) _
    
                                       Select item
    the warning is "using iteration variable in a query expression may have unexpected result"

    what should i do about this? is it ok if i just disregard it?

    many thanks dude, ive learned much from you.

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    it should work ok. you can get rid of the warning by using another variable:

    vb Code:
    1. For Each foundFile As String In IO.Directory.GetFiles(fbd.SelectedPath, "*.wav", IO.SearchOption.AllDirectories)
    2.     dim tempVar as string = foundFile
    3.     Dim contains = From item In ListBox1.Items _
    4.                    Where tempVar.Contains(item.ToString) _
    5.                    Select item

  13. #13

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    i see.. thanks again! very last last thing!

    how can i copy all of the items that are listed in listbox2 to a folder?

    can u correct the code that i have?
    Code:
    System.IO.File.Copy(listbox2.Items, fbCopyto.SelectedPath)

  14. #14
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    try this. assuming listbox2 contains full file paths:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Using fbCopyto As New FolderBrowserDialog
    5.             If fbCopyto.ShowDialog = Windows.Forms.DialogResult.OK Then
    6.                 For Each file As String In ListBox2.Items
    7.                     IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
    8.                 Next
    9.             End If
    10.         End Using
    11.        
    12.     End Sub
    13. End Class

  15. #15

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Resolved Re: help about listbox!!

    all done!! thank you very much!

  16. #16

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    hey dude, do you also know how to extract listbox3 into a excel file?

  17. #17

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    paul, can you help me with this one, i want to rename the file if the file name exists.. i cant seem to work out this code.


    Dim x As Integer

    x = 1
    Code:
    Try
                    For Each file As String In listbox1.Items
                        IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
                        While IO.File.Exists(fbCopyto.SelectedPath & file)
                            file=  file & x & ".wav"
                            x += 1
    
    
                        End While
                    Next
    Last edited by markirving; Sep 23rd, 2010 at 04:11 PM.

  18. #18
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    can you post the full latest code you're using + i'll have a look

  19. #19

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    thanks for the reply paul, here is the latest code for the search button.

    Code:
    Dim x As Integer
    x = 1
    
    Try
                    For Each file As String In listbox1.Items
                        IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
                        if IO.File.Exists(fbCopyto.SelectedPath & file)
                            file=  file & x & ".wav"
                            x += 1
    
    
                       endif 
                    Next
    Last edited by markirving; Sep 24th, 2010 at 03:18 PM.

  20. #20
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    i can't understand how it fits in with the rest of the code from that.
    i'd have a better idea of what you're trying to do if you post the full code.

  21. #21

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    actually paul, this is the full code of my button. what id like is when for example, batman.wav has already a copy or already existed in the selected path, it will try to copy another file name which will be named batman(1).wav

    Code:
    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click
          
    Dim x As Integer
    x = 1
    
    
            
                Try
                    For Each file As String In listbox1.Items
                        IO.File.Copy(file, IO.Path.Combine(fbCopyto.SelectedPath, IO.Path.GetFileName(file)))
                        If IO.File.Exists(file) = True Then
                         file=  file & x & ".wav"
                            x += 1
    
    
                        End If
                    Next
    
                    MsgBox("Copied!!")
    
    
                    lbFound.Items.Clear()
                    lbNot.Items.Clear()
    
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        End Sub

  22. #22
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    vb Code:
    1. Try
    2.     'this is assuming ListBox1.Items are full file paths
    3.     'which if i remember correctly they aren't
    4.     For Each file As String In ListBox1.Items
    5.         'here you'll need to convert file into a full file path
    6.         Dim x As Integer = 1
    7.         Dim dirPath As String = IO.Path.GetDirectoryName(file)
    8.         Dim filename As String = IO.Path.GetFileNameWithoutExtension(file)
    9.         Dim tempFilename As String = IO.Path.Combine(dirPath, filename & ".wav")
    10.         Do While IO.File.Exists(tempFilename)
    11.             tempFilename = IO.Path.Combine(dirPath, filename & "(" & x.ToString & ").wav")
    12.             x += 1
    13.         Loop
    14.         IO.File.Copy(file, tempFilename)
    15.     Next
    16.  
    17.     MsgBox("Copied!!")
    18.  
    19.  
    20.     lbFound.Items.Clear()
    21.     lbNot.Items.Clear()
    22.  
    23. Catch ex As Exception
    24.     MsgBox(ex.Message)
    25. End Try

  23. #23

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    wow man. thats great. may i know how do you come up with your codes? do you have an e-book or something?

  24. #24

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    very last problem, how can i display in listbox3 the all the items that does not have any match from listbox1'


    for example, listbox1 has : batman.wav, superman.wav, ironman.wav, spiderman.wav

    if it finds its match suppose batman.wav is the only things that has a match, it will be displayed on listbox2(which we already did)

    then if not, superman.wav, ironman.wav, spiderman.wav will be displayed on listbox3

    when i try to use this if then else statement, the items being displayed on listbox3 are the other .wav files inside the folder. do u have a solution for this one also?

    Code:
    If contains.Count > 0 Then
    
                    'to add filenames without extensions
    
                    'lisbox2.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    
    
                    lisbox2.Items.Add(foundFile)
    
    else
    
                        listbox3.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    
                        'listbox3.Items.Add(foundFile)
    
                    
                End If
    this is my current code for the search button:

    Code:
    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    
    
    
            For Each foundFile As String In IO.Directory.GetFiles(txtSearch.Text, "*.wav", IO.SearchOption.AllDirectories)
    
                Dim tempvar As String = foundFile
                Dim contains = From item In listbox1.Items _
                               Where tempvar.Contains(item.ToString) Select item
               
    
    
                If contains.Count > 0 Then
    
                    'to add filenames without extensions
    
                    'lisbox2.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    
    
                    lisbox2.Items.Add(foundFile)
    
    
                    If 'a line that displays all the items that does not have any match from listbox1' Then
    
                        listbox3.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    
                        'listbox3.Items.Add(foundFile)
    
                    End If
                End If
    
    
    
    
            Next
    
           
        End Sub

  25. #25
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    Quote Originally Posted by markirving View Post
    wow man. thats great. may i know how do you come up with your codes? do you have an e-book or something?
    it's just experience + (sometimes) research online

  26. #26
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    vb Code:
    1. Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
    2.  
    3.  
    4.  
    5.     For Each foundFile As String In IO.Directory.GetFiles(txtSearch.Text, "*.wav", IO.SearchOption.AllDirectories)
    6.  
    7.         Dim tempvar As String = foundFile
    8.         Dim contains = From item In listbox1.Items _
    9.                        Where tempvar.Contains(item.ToString) Select item
    10.  
    11.         If contains.Count > 0 Then
    12.             'to add filenames without extensions
    13.             'lisbox2.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    14.             ListBox2.Items.Add(foundFile)
    15.         Else 'a line that displays all the items that does not have any match from listbox1' Then
    16.             ListBox3.Items.Add(IO.Path.GetFileNameWithoutExtension(foundFile))
    17.             'listbox3.Items.Add(foundFile)
    18.         End If
    19.  
    20.     Next
    21.  
    22.     For Each item1 As String In ListBox1.Items
    23.         Dim found As Boolean = False
    24.         For Each item2 As String In ListBox2.Items
    25.             If item2.Contains(item1) Then
    26.                 found = True
    27.                 Exit For
    28.             End If
    29.         Next
    30.         If Not found Then ListBox3.Items.Add(item1)
    31.     Next
    32.    
    33.  
    34. End Sub

  27. #27

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    do you have a site that you can recommend me? i really want to be better using vb.net!


    i have tried this code. the problem is that it found for example superman_opening.wav on listbox2 but still superman is still displayed on listbox3.. the weird thing that the first item in listbox3 has the correct output. (found batman_opening.wav in listbox2 and then did not display batman in listbox3)


    Code:
      For Each item1 As String In ListBox1.Items
    
            Dim found As Boolean = False
    
            For Each item2 As String In ListBox2.Items
    
                If item2.Contains(item1) Then
    
                    found = True
    
                    Exit For
    
                End If
    
            Next
    
            If Not found Then ListBox3.Items.Add(item1)
    
        Next

  28. #28
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    try www.homeandlearn.co.uk

    what is the exact format of the list in listbox1?

  29. #29

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    what do u mean exact format?

    what i did there is that it opens a csv file then gets the column of the excel file and then the column will be the items listed in listbox1.

  30. #30

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    this you start from www.homeandlearn.co.uk as well?

  31. #31
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    in listbox1 is it batman or batman.wav?
    i assumed after reading your earlier questions that you wanted the items in listbox1 that hadn't been added to listbox2 should be added to listbox3?

  32. #32

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    batman only. yes your correct.

    i tried to debug the code:

    Code:
      For Each item1 As String In ListBox1.Items
    
            Dim found As Boolean = False
    
            For Each item2 As String In ListBox2.Items
    
                If item2.Contains(item1) Then
    
                    found = True
    
                    Exit For
    
                End If
    
            Next
    
            If Not found Then ListBox3.Items.Add(item1)
    
        Next
    i have noticed that item2 never changes its value, it is always ("c:\batman.wav") maybe that is the reason why after batman.wav (first item in listbox1) the items after batman.wav like superman displays listbox3 even though superman.wav is added on listbox2

  33. #33
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    vb Code:
    1. For Each item1 As String In ListBox1.Items
    2.  
    3.         Dim found As Boolean = False
    4.  
    5.         For Each item2 As String In ListBox2.Items
    6.  
    7.             If item2.Contains(IO.Path.GetFileNameWithoutExtension(item1)) Then
    8.  
    9.                 found = True
    10.  
    11.                 Exit For
    12.  
    13.             End If
    14.  
    15.         Next
    16.  
    17.         If Not found Then ListBox3.Items.Add(item1)
    18.  
    19.     Next

    if you read through it step by step you'll see that it first loops through listbox1.items, then for each item loops through listbox2.items + checks them.

    i didn't learn programming @ www.homeandlearn.co.uk, i've been programming since the early 90s + it wasn't around then, but it's a good place to learn

  34. #34

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    i see..i got a long way ahead then. lol

    same output dude, hmm.. why does item2 does not change its value even though it has finished 1 loop, item2 still does not change but item1 does.

  35. #35
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: help about listbox!!

    it works ok for me. here's an example project:
    Attached Files Attached Files

  36. #36

    Thread Starter
    Member
    Join Date
    Jul 2010
    Posts
    53

    Re: help about listbox!!

    hey paul. fixed the problem, i forgot to put next before the loop. thank you very much for your help. i hope to be a good vb programmer like you. thanks again and good luck to your projects!

Tags for this Thread

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