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
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.
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.
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?
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.
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.
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
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
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
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?
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
If item2.Contains(IO.Path.GetFileNameWithoutExtension(item1)) Then
found = True
Exit For
End If
Next
If Not found Then ListBox3.Items.Add(item1)
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
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!