Results 1 to 27 of 27

Thread: [RESOLVED] Add file name to list box but keep file path in RAM

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Resolved [RESOLVED] Add file name to list box but keep file path in RAM

    How do I add a file name to a list box, but keep that file's path in memory? The list box will be populated with lots of files so I will need help!!!!!
    If anyone knows how to get around this, please respond ASAP!!!!
    Louix.

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    not sure what you mean by "Keep file path in memory"
    in case if you want drag and drop file name to listbox you can use

    Code:
    Private Sub ListBox1_DragEnter(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DragEventArgs) _
                Handles ListBox1.DragEnter
    
            If (e.Data.GetDataPresent(DataFormats.FileDrop) = True) Then
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
    
        Private Sub ListBox1_DragDrop(ByVal sender As Object, _
              ByVal e As System.Windows.Forms.DragEventArgs) _
              Handles ListBox1.DragDrop
            Dim oneFile As String
            For Each oneFile In _
                  e.Data.GetData(DataFormats.FileDrop)
                ListBox1.Items.Add(oneFile)
            Next oneFile
        End Sub
    make sure allowdrop property of listbox is set to True
    __________________
    Rate the posts that helped you

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    Well you helped me with a feature I wanted to add lol, but what I mean by keeping the file path in Memory is storing the file's path and make it accessable by the application later on.
    Thank you for the quick reply

    EDIT: Drag-drop doesn't work but that's for another thread
    Last edited by Louix; Jul 22nd, 2008 at 01:11 PM.

  4. #4
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    ahh ok got it...may be you can use Form level datatable or sortedlist or hashtable to stored "FileName" "FilePath" combination
    __________________
    Rate the posts that helped you

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

    Re: Add file name to list box but keep file path in RAM

    try this. you'll have to modify it if you're getting many files from different directories.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim files() As String = IO.Directory.GetFiles("c:\windows")
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         For x As Integer = 0 To files.GetUpperBound(0)
    7.             ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    8.         Next
    9.     End Sub
    10.  
    11.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    12.         MsgBox(files(ListBox1.SelectedIndex))
    13.     End Sub
    14.  
    15. End Class

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Unhappy Re: Add file name to list box but keep file path in RAM

    Thank you for the quick reply
    I modified your code to look something like this:
    Code:
          Public Class Form1
    
              Dim files() As String = OpenFileDialog1.FileNames
    
              Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
                  For x As Integer = 0 To files.GetUpperBound(0)
     
                      ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    
                  Next
     
              End Sub
      
           
      
              Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
      
                  MsgBox(files(ListBox1.SelectedIndex))
      
              End Sub
      
           
      
          End Class
    But I keep getting the "NullReferenceException"

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

    Re: Add file name to list box but keep file path in RAM

    ok try this

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim files() As String
    4.  
    5.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    6.         MsgBox(files(ListBox1.SelectedIndex))
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    11.             files = OpenFileDialog1.FileNames
    12.             ListBox1.Items.Clear()
    13.             For x As Integer = 0 To files.GetUpperBound(0)
    14.                 ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    15.             Next
    16.         End If
    17.     End Sub
    18.  
    19. End Class

  8. #8
    Member
    Join Date
    Dec 2007
    Posts
    34

    Re: Add file name to list box but keep file path in RAM

    The neat thing about listboxes is that they can store objects, not just integers, strings, etc etc.

    One of the things that I like doing is creating a custom object and storing that object in the listbox. Then you can use ToString() to have the listbox item "show" the filename, but also store the filepath. For instance, the object would look something like:

    Code:
    Public Class Store
    
    Public FileName As String
    Public FileLocation As String
    
    Public Sub Overrides ToString()
         return Me.FileName
    End Sub
    
    End Class
    Now when you add this object to the listbox, it will display the object FileName, but the FileLocation is also still accessible.

  9. #9
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    use this

    Code:
    Public Class Form1
    
              Dim files() As String  
              Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
             OpenFileDialog1.ShowDialog()
    
            files = OpenFileDialog1.FileNames
                  For x As Integer = 0 To files.GetUpperBound(0)
     
                      ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    
                  Next
     
              End Sub
      
           
      
              Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
      
                  MsgBox(files(ListBox1.SelectedIndex))
      
              End Sub
      
           
      
          End Class
    __________________
    Rate the posts that helped you

  10. #10
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    opps i was too late
    __________________
    Rate the posts that helped you

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Add file name to list box but keep file path in RAM

    I'd declare a Structure or Class with all the necessary members and use it for items in the listbox... See post#8 by NewtonsBit for example, however, I'd create 2 readonly properties (fileName and filePath), and override the ToString method just like he did. It should have a contructor that takes a string which is the full path to a file (and it should check for the file existence too).
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    I have just tried .paul. & riteshjain's code but when multiple items are clicked on, I get the "IndexOutOfRangeException" error!!!!
    It's not that I'm trying to be ignorant, but I am thick at (complex) things like this and prefer to learn from seeing code and learning how that works.

  13. #13
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    at what line you are getting

    "IndexOutOfRangeException" error!!!!
    __________________
    Rate the posts that helped you

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    Code:
    MsgBox(files(ListBox1.SelectedIndex))

  15. #15
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    few quries
    1>Make sure files() is form level variable and not local.
    2>Does you ListBox has same no. of item as that in files() array?
    3>Make sure that you are calling ListBox1.Items.Clear() before adding item in it
    __________________
    Rate the posts that helped you

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    1. files() is a form-level variable.
    2. I thought you could put as many items as you want in the files() array.
    3. I want the user to be able to select multiple items, therefore I removed ListBox1.Items.Clear() from the code.

  17. #17
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    can you post your latest code?
    __________________
    Rate the posts that helped you

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    Code:
    Public Class Form1
    
    
    
        Dim files() As String
    
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
            MsgBox(files(ListBox1.SelectedIndex))
    
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    
                files = OpenFileDialog1.FileNames
    
    
                For x As Integer = 0 To files.GetUpperBound(0)
    
                    ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    
                Next
    
            End If
    
        End Sub
    
    End Class
    As I said, I removed the ListBox1.Items.Clear() function.

  19. #19
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    as your files() array always getting override so you need to preserve it's value (as you are not clearing you ListBox item),so if you want to continue with your method you can use


    Code:
    Public Class Form1
    
    
    
        Dim files() As String
    
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
            MsgBox(files(ListBox1.SelectedIndex))
    
        End Sub
     
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
               Dim arrTemp() As String
                arrTemp= OpenFileDialog1.FileNames
            If files Is Nothing Then
                files = arrTemp
            Else
                ReDim Preserve files(files.Length + arrTemp.Length - 1)
             End If
            Dim x As Integer
            For x = 0 To arrTemp.GetUpperBound(0)
                ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
                files(files.Length - arrTemp.Length + x) = arrTemp(x)        Next
            End If
    
        End Sub
    
    End Class
    __________________
    Rate the posts that helped you

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    I think that we're nearly there!!!
    Adding multiple items works and preserves their file paths - but now the problem is that any items added afer the first, have the first item's name .

  21. #21
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    have the first item's name what you mean by this.....isn't that you wanted it to be there (as you dont want clean listbox data)
    __________________
    Rate the posts that helped you

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    I want to be able to open files, add their [own] names to a listbox but keep their [own] paths in memory. You've done most of the work correctly, and for that, I thank you very very much. But there's a bug, and that bug is add the first item and keep it's path, add another item, get the first item's name and keep the new item's path in memory.

    Would you like me to YouTube this so I can show you what I mean?

  23. #23
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: Add file name to list box but keep file path in RAM

    ohh yea got it

    change this line

    Code:
      ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
    
    to
    
       ListBox1.Items.Add(IO.Path.GetFileName(arrTemp(x)))
    __________________
    Rate the posts that helped you

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

    Re: Add file name to list box but keep file path in RAM

    try this. i think it will solve the problem

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim filesList As New List(Of String)
    4.  
    5.     Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    6.         If ListBox1.SelectedIndices.Contains(ListBox1.IndexFromPoint(New Point(e.X, e.Y))) Then
    7.             MsgBox(filesList(ListBox1.IndexFromPoint(New Point(e.X, e.Y))))
    8.         End If
    9.     End Sub
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    13.             Dim files() As String = OpenFileDialog1.FileNames
    14.             ListBox1.Items.Clear()
    15.             For x As Integer = 0 To files.GetUpperBound(0)
    16.                 filesList.Add(files(x))
    17.             Next
    18.             For x As Integer = 0 To filesList.Count - 1
    19.                 ListBox1.Items.Add(IO.Path.GetFileName(filesList(x)))
    20.             Next
    21.         End If
    22.     End Sub
    23.  
    24. End Class

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    219

    Re: Add file name to list box but keep file path in RAM

    That's exactly what I need!!! Thank you all very very very much for all your help!

  26. #26
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [RESOLVED] Add file name to list box but keep file path in RAM

    The problem with using a separate file List is that, if you remove an item from your listbox, or if your re-arrange the item's order in your listbox, you have to write additional code to remove/rearrange the corresponding entry in the file list or else your file list and the listbox won't be in sync any more.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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

    Re: [RESOLVED] Add file name to list box but keep file path in RAM

    stanav's right. it would be better to use a custom listitem.

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
    4.         If ListBox1.SelectedIndices.Contains(ListBox1.IndexFromPoint(New Point(e.X, e.Y))) Then
    5.             MsgBox(DirectCast(ListBox1.Items(ListBox1.IndexFromPoint(New Point(e.X, e.Y))), listItem).filePath)
    6.         End If
    7.     End Sub
    8.  
    9.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    11.             Dim files() As String = OpenFileDialog1.FileNames
    12.             For x As Integer = 0 To files.GetUpperBound(0)
    13.                 Dim li As New listItem(IO.Path.GetFileName(files(x)), files(x))
    14.                 ListBox1.Items.Add(li)
    15.             Next
    16.         End If
    17.     End Sub
    18.  
    19. End Class
    20.  
    21. Public Class listItem
    22.  
    23.     Public fileName As String
    24.     Public filePath As String
    25.  
    26.     Public Overrides Function ToString() As String
    27.         Return Me.filename
    28.     End Function
    29.  
    30.     Public Sub New(ByVal fileName As String, ByVal filePath As String)
    31.         Me.fileName = fileName
    32.         Me.filePath = filePath
    33.     End Sub
    34.  
    35. End Class

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