Listbox help.

Worf

Newcomer
Joined
Dec 19, 2008
Messages
17
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 are not shown in the listboxs, 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 added to listbox2 to a new folder.

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.

The files added to listbox2 and the paths to listbox4 are different and i can't seem to get the correct paths added to listbox4 for the items added to listox2.

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

Search routine:
Code:
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

Code:
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


have tried different ways without any luck.


Many Thanks to those who can help.


Worf
 
I would suggest creating a seperate class for the paths with a collection class.
I'm sorry I haven't got the time right now, but I will take a look on this later today ;)
Good luck
 
Hi DPrometheus

Thank you for your reply. I would be very greatful if you could take a look at it for me, i have been on this for just over a week now and just want to get this working so that i can get on with the next part of my application.



Many Thanks

Worf
 
Here is some snippet
Create a form with 3 buttons, 2 listboxes and a textbox
I've called my form tForm, the controls have it's default names

Code:
Imports System.Collections.ObjectModel

''' <summary>
''' Form class
''' </summary>
''' <remarks></remarks>
Public Class tForm
    ''' <summary>
    ''' Scan button
    ''' </summary>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.ContextMenuChanged, Button2.Click
        If FolderBrowserDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ' Add scan folder to textbox
            TextBox1.Text = FolderBrowserDialog.SelectedPath
            ' Get all files in folder
            Dim files As ReadOnlyCollection(Of String)
            files = My.Computer.FileSystem.GetFiles(TextBox1.Text, FileIO.SearchOption.SearchAllSubDirectories, "*.bmp")
            ' Add each file to the listbox
            For Each s As String In files
                Dim i As New Item(s)
                ListBox1.Items.Add(i)
            Next
        End If
    End Sub

    ''' <summary>
    ''' Add file to second listbox
    ''' </summary>
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox2.Items.Add(ListBox1.SelectedItem)
    End Sub

    ''' <summary>
    ''' The save button
    ''' </summary>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' Copies all items into a temporary newFolder folder
        For Each i As Item In ListBox2.Items
            My.Computer.FileSystem.CopyFile(i.name, "newFolder\" + i.shortname)
        Next
    End Sub

    ''' <summary>
    ''' Load form
    ''' </summary>
    Private Sub tForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Set listbox to display the property shortname
        ' but use the value of the name property
        ListBox1.ValueMember = "name"
        ListBox1.DisplayMember = "shortname"

        ListBox2.ValueMember = "name"
        ListBox2.DisplayMember = "shortname"
    End Sub
End Class

''' <summary>
''' Seperate class for listbox values
''' </summary>
''' <remarks></remarks>
Public Class Item
    ' Path to the file
    Private _longname As String

    ''' <summary>
    ''' Stripped of string
    ''' Just the filename
    ''' </summary>
    Public ReadOnly Property shortname() As String
        Get
            Dim _str As String
            Dim idx As Int32 = _longname.LastIndexOf("\") + 1
            Dim len As Int32 = _longname.Length
            _str = _longname.Substring(idx, len - idx)
            Return _str
        End Get
    End Property

    ''' <summary>
    ''' The whole path
    ''' </summary>
    Public Property name() As String
        Get
            Return _longname
        End Get
        Set(ByVal value As String)
            _longname = value
        End Set
    End Property

    ''' <summary>
    ''' Constructor
    ''' </summary>
    ''' <param name="n">path to the file</param>
    ''' <remarks></remarks>
    Public Sub New(ByVal n As String)
        name = n
    End Sub
End Class

hope it's clear
It scans a folder puts all files ending with .bmp in a listbox (modify .bmp to .wav to fit your needs)
An add button is provided to put files into the other listbox
and a save button saves all files added to the secondary listbox into
$workingfolder\newFolder\
 
http://www.xtremedotnettalk.com/showthread.php?t=94476 might be worth a quick skim as it has an example of a class used to wrap a file path as suggested by DPrometheus earlier.

Scroll down to
Public Class Item
to see the class I was talking about ;)

keypoint is.. Use the members ValueMember to show items and DisplayMember to hold data.
The data behind a listbox uses a type called Object which can be used for almost any custom class you create.
 
Re: Listbox help. [SOLVED]

Thank you for the Snippet, it works great :D now i can get on with my application.

Once again thank you.
 
Back
Top