-
[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.
-
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
-
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 :)
-
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
-
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:
Public Class Form1
Dim files() As String = IO.Directory.GetFiles("c:\windows")
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
-
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" :(
-
Re: Add file name to list box but keep file path in RAM
ok try this
vb 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
ListBox1.Items.Clear()
For x As Integer = 0 To files.GetUpperBound(0)
ListBox1.Items.Add(IO.Path.GetFileName(files(x)))
Next
End If
End Sub
End Class
-
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.
-
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
-
Re: Add file name to list box but keep file path in RAM
-
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).
-
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.
-
Re: Add file name to list box but keep file path in RAM
at what line you are getting
"IndexOutOfRangeException" error!!!!
-
Re: Add file name to list box but keep file path in RAM
Code:
MsgBox(files(ListBox1.SelectedIndex))
-
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
-
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.
-
Re: Add file name to list box but keep file path in RAM
can you post your latest code?
-
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.
-
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
-
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 :(.
-
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)
-
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?
-
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)))
-
Re: Add file name to list box but keep file path in RAM
try this. i think it will solve the problem
vb Code:
Public Class Form1
Dim filesList As New List(Of String)
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If ListBox1.SelectedIndices.Contains(ListBox1.IndexFromPoint(New Point(e.X, e.Y))) Then
MsgBox(filesList(ListBox1.IndexFromPoint(New Point(e.X, e.Y))))
End If
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 files() As String = OpenFileDialog1.FileNames
ListBox1.Items.Clear()
For x As Integer = 0 To files.GetUpperBound(0)
filesList.Add(files(x))
Next
For x As Integer = 0 To filesList.Count - 1
ListBox1.Items.Add(IO.Path.GetFileName(filesList(x)))
Next
End If
End Sub
End Class
-
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!
-
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.
-
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:
Public Class Form1
Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
If ListBox1.SelectedIndices.Contains(ListBox1.IndexFromPoint(New Point(e.X, e.Y))) Then
MsgBox(DirectCast(ListBox1.Items(ListBox1.IndexFromPoint(New Point(e.X, e.Y))), listItem).filePath)
End If
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 files() As String = OpenFileDialog1.FileNames
For x As Integer = 0 To files.GetUpperBound(0)
Dim li As New listItem(IO.Path.GetFileName(files(x)), files(x))
ListBox1.Items.Add(li)
Next
End If
End Sub
End Class
Public Class listItem
Public fileName As String
Public filePath As String
Public Overrides Function ToString() As String
Return Me.filename
End Function
Public Sub New(ByVal fileName As String, ByVal filePath As String)
Me.fileName = fileName
Me.filePath = filePath
End Sub
End Class