3 Replies - 543 Views - Last Post: 28 January 2009 - 10:25 AM Rate Topic: -----

#1 Worf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 08

Please Help with this code

Posted 27 January 2009 - 01:42 PM

Hi.

I am a newbie when it comes to programming and what i have done so far with my application is through sample codes, tutorials and with the help of fellow programers.

I am trying to pass data and file paths from one listbox1 to listbox2, now i can pass data from listbox1 to listbox2 but i am having problems with the paths to the files listed in the listbox's. The paths to the files listed in the listboxs are not shown, only there filenames.

My program will search a selected drive or folder for .wav files and list any found in listbox1 without there paths, (only there file names), now i have an add button so that the user can select an item in listbox1 and add it to listbox2. Now what i need to be able to do is to save the .wav files add to listbox2 to a folder which is coded in the application.

Now what i have tried so far is to add another two listbox's called listbox3 and listbox4 which are both hidden from the user.
Listbox3 holds the paths to all the .wav files found for listbox1 and listbox4 will hold the paths to the .wav files added to listbox2.
Listbox3 adds the paths to listbox4 when items are added to listbox2 from listbox1.


Below is the code that i have been trying to use.


Search routine:

Public Sub FileTypeFinder(ByVal dir As String, ByVal fileExt As String)  
	  Try		  

  ' Display all files in a directory that match file type	   
	 For Each fname As String In IO.Directory.GetFiles(dir)  
			  If fname.EndsWith(fileExt) Then				 
   ListBoxControl1.Items.Add(My.Computer.FileSystem.GetName(fname))	 
			   listBoxControl3.Items.Add(fname)	
					  
 End If		   
 Next		

	' A recursive call for all the subdirectories in this directory.	

		For Each subdir As String In IO.Directory.GetDirectories(dir)  
			  FileTypeFinder(subdir, fileExt)		 
   Next	  

  Catch ioex As System.UnauthorizedAccessException

		Catch ex As Exception			
MessageBox.Show(ex.Message.ToString)		
	  End Try   

 End Sub




Add Routine: Add selected items from listbox1 to listbox2


Public Sub Add_ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add_ButtonX1.Click 

	   Select Case ListBoxControl1.Items.Count 
		   Case 0  
			  MsgBox("There is nothing to add. Please browse for wav files.")   
			 Return

		End Select 
	   Try 

		  ' Add the paths to the wav files from listbox3 to listbox4  

					  ListBoxControl4.Items.Add(ListBoxControl3.SelectedItem)  

		  ' Remove the selected items path from listbox3  

						ListBoxControl3.Items.Remove(ListBoxControl3.SelectedItem)   

		' Add the selected items from listbox1 to listbox2 

					  ListBoxControl2.Items.Add(ListBoxControl1.SelectedItem) 

		   ' Remove the selected item from listbox1 

					  ListBoxControl1.Items.Remove(ListBoxControl1.SelectedItem) 

		   ' Save the .wav items in listbox2 using the paths in listbox4

		   For list2 As Integer = 0 To Me.ListBoxControl2.Items.Count - 1

						   System.IO.File.Copy(ListBoxControl4.SelectedItems(list2), "C:\SFE\Temp\Sounds\" + (My.Computer.FileSystem.GetName(ListBoxControl4.Items(list2))))

			Next 
				Catch ex As Exception

			MsgBox(ex.Message)  
	  End Try
	End Sub





I have tried different ways without any luck.


Many Thanks to those who can help.


Worf

Is This A Good Question/Topic? 0
  • +

Replies To: Please Help with this code

#2 scalt  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 342
  • Joined: 22-November 07

Re: Please Help with this code

Posted 27 January 2009 - 08:29 PM

What are the other list boxes (3 and 4) doing?

For getting the files, I would do:
        For Each fname As String In IO.Directory.GetFiles(dir, fileExt, SearchOption.AllDirectories)

ListBoxControl1.Items.Add(fname)

        Next



That will search through your dir and all subsequent sub-dirs for any '.wav' files, then loop through the array of filenames (incl path) and add them to ListBoxControl1.
Was This Post Helpful? 0
  • +
  • -

#3 Worf  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 29-November 08

Re: Please Help with this code

Posted 28 January 2009 - 09:36 AM

View Postscalt, on 27 Jan, 2009 - 07:29 PM, said:

What are the other list boxes (3 and 4) doing?

For getting the files, I would do:
        For Each fname As String In IO.Directory.GetFiles(dir, fileExt, SearchOption.AllDirectories)

ListBoxControl1.Items.Add(fname)

        Next




Hi scalt.

Thank you for your reply. The above is basicly what i use and the only problem with the above is that it will also display the path to the wav files which is what i don't want, i just want the file names displayed in listbox1.

3 was to hold the path to each wav file found and 4 was the paths to the wav files added to listbox2.
So when the user selects a filename in listbox1 and then clicks the add button it would be added from listbox1 to listbox2 and the path to that wav file would be copied from listbox3 to listbox4 so that the wav files could be saved to a folder ready for when they are needed again by the application.

This post has been edited by Worf: 28 January 2009 - 09:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 160
  • Joined: 01-August 08

Re: Please Help with this code

Posted 28 January 2009 - 10:25 AM

Hi Worf,

If you're looking to get the file name without the directory, try:

Dim someVariable as string = System.IO.Path.GetFileName(fname)


Then add your someVariable to the list box.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1