Results 1 to 10 of 10

Thread: [2008] open file dialog

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    [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

  2. #2
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    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
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    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

  4. #4
    Frenzied Member CoachBarker's Avatar
    Join Date
    Aug 2007
    Location
    Central NY State
    Posts
    1,121

    Re: [2008] open file dialog

    So what is your open file dialog code, post it.
    Thanks
    CoachBarker

    Code Bank Contribution
    Login/Manage Users/Navigate Records
    VB.Net | C#

    Helpful Links: VB.net Tutorial | C Sharp Tutorial | SQL Basics

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

    Re: [2008] open file dialog

    try this:

    vb Code:
    1. Dim ofd As New OpenFileDialog
    2. ofd.Multiselect = True
    3. If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    4.   For Each filename As String In ofd.FileNames
    5.       ListBox1.Items.Add(IO.Path.GetFileName(filename))
    6.   Next
    7. End If

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

    Re: [2008] open file dialog

    but you need the full filename to open them, don't you?

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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

    Re: [2008] open file dialog

    you could use a custom listitem, like this:

    vb Code:
    1. Public Class fileListItem
    2.     Public display As String
    3.     Public fileName As String
    4.  
    5.     Public Overrides Function ToString() As String
    6.         Return Me.display
    7.     End Function
    8.  
    9.     Public Sub New(ByVal display As String, ByVal fileName As String)
    10.         Me.display = display
    11.         Me.fileName = fileName
    12.     End Sub
    13. End Class

    then to use it:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    4.         Dim ofd As New OpenFileDialog
    5.         ofd.Multiselect = True
    6.         If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             For Each filename As String In ofd.FileNames
    8.                 ListBox1.Items.Add(New fileListItem(IO.Path.GetFileNameWithoutExtension(filename), filename))
    9.             Next
    10.         End If
    11.     End Sub
    12.  
    13.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    14.         MsgBox(DirectCast(ListBox1.SelectedItem, fileListItem).fileName)
    15.     End Sub
    16.  
    17. End Class

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2008
    Posts
    413

    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.

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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

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