|
-
Nov 23rd, 2008, 08:21 AM
#1
Thread Starter
Hyperactive Member
[2008] open file dialog
me again with something a little different this time.
I have made an "mp3 player" in which i use the open file dialog to open multipal mp3 files. and a list box to list the files that are open... but it keeps opening the files and displaying the full file path and not just the name... so my question is, how would I get it to just disply the file name???
Dan
-
Nov 23rd, 2008, 08:35 AM
#2
Frenzied Member
Re: [2008] open file dialog
Something like this
Code:
' declare a global variable to hold the name of the data base being used
Dim selectedDatabase As String
' varaible to hold the path to the data base
Dim dataBasePath As String = Me.selectedDatabase
' the actual name of the data base
Dim dataBaseName As String = IO.Path.GetFileName(dataBasePath)
from a project I created
-
Nov 23rd, 2008, 08:38 AM
#3
Thread Starter
Hyperactive Member
Re: [2008] open file dialog
hmmm... i am not sure i understood that. I am a complete programming noob. Dont know to much about it
-
Nov 23rd, 2008, 08:43 AM
#4
Frenzied Member
Re: [2008] open file dialog
So what is your open file dialog code, post it.
-
Nov 23rd, 2008, 08:48 AM
#5
Re: [2008] open file dialog
try this:
vb Code:
Dim ofd As New OpenFileDialog
ofd.Multiselect = True
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each filename As String In ofd.FileNames
ListBox1.Items.Add(IO.Path.GetFileName(filename))
Next
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 23rd, 2008, 08:52 AM
#6
Re: [2008] open file dialog
but you need the full filename to open them, don't you?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 23rd, 2008, 09:00 AM
#7
Re: [2008] open file dialog
should look something like this
Code:
Dim openFD As New OpenFileDialog(), openFDr As DialogResult
openFD.InitialDirectory = "c:\"
openFD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFD.FilterIndex = 2
openFD.RestoreDirectory = True
openFD.Multiselect = True
openFDr = openFD.ShowDialog
If openFDr = Windows.Forms.DialogResult.OK Then
For Each fn As String In openFD.FileNames
ListBox3.Items.Add(IO.Path.GetFileNameWithoutExtension(fn))
Next
End If
-
Nov 23rd, 2008, 09:01 AM
#8
Re: [2008] open file dialog
you could use a custom listitem, like this:
vb Code:
Public Class fileListItem
Public display As String
Public fileName As String
Public Overrides Function ToString() As String
Return Me.display
End Function
Public Sub New(ByVal display As String, ByVal fileName As String)
Me.display = display
Me.fileName = fileName
End Sub
End Class
then to use it:
vb Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
ofd.Multiselect = True
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
For Each filename As String In ofd.FileNames
ListBox1.Items.Add(New fileListItem(IO.Path.GetFileNameWithoutExtension(filename), filename))
Next
End If
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(DirectCast(ListBox1.SelectedItem, fileListItem).fileName)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 23rd, 2008, 09:04 AM
#9
Thread Starter
Hyperactive Member
Re: [2008] open file dialog
this is the coding that I have already, which works... but it displays the file path
Code:
Private Sub Importdiag_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Importdiag.FileOk
For Each track As String In Importdiag.FileNames
Playlist.Items.Add(track)
Next
End Sub
I will try what you said Paul
edit: sorry I forgot the coding
Last edited by danielpalfrey; Nov 23rd, 2008 at 09:17 AM.
-
Nov 23rd, 2008, 10:56 AM
#10
Re: [2008] open file dialog
Instead of
Code:
Playlist.Items.Add(track)
Use
Code:
Playlist.Items.Add(IO.Path.GetFileNameWithoutExtension(track))
Now however, you can no longer use the items in the Listbox to get the full path (for example for actually playing them). If this is OK then the code above will do.
If you still need the full path but only display the filename you can use any of the above suggestions.
Also, you could use a ListView instead of a ListBox, and set the "View" property to "List".
Then, assuming the Listview is called "Playlist" you can use:
Code:
Dim lvi As New ListViewItem(IO.Path.GetFileNameWithoutExtension(track))
lvi.Tag = track
Playlist.Items.Add(lvi)
What that does is create a new ListViewItem (lvi) which displays the Filename of 'track' only, but stores the actual full path (track) in it's Tag property.
To access the full path now you can use:
Code:
lvi.Tag.ToString
'or
CType(lvi.Tag, String)
'example:
Dim lvi As ListViewItem = PlayList.SelectedItem
Dim fullPath As String = lvi.Tag.ToString
Last edited by NickThissen; Nov 23rd, 2008 at 11:01 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|