2011-10-23 15 views
0

私は音楽プレーヤーアプリケーションを作成しています。私は曲を表示するためにListBoxを使用しています。ユーザーが曲を追加すると、その曲のフルパスが表示されます。しかし、私はそれが歌の名前を表示するだけです(曲は任意のドライブの任意のフォルダに置くことができます)。 Windows Media Playerコントロールが曲を再生しています。前もって感謝します!ファイルのパスをリストボックスに保存する

+0

あなたはここでSadiaを使用しているプレゼンテーションフレームワークを言う必要があります。それはWPF/Silverlightかもしれないように聞こえるが、それはWinformsまたは何か他のものかもしれない。また、より簡単に役立つ人々のためのいくつかのコードを貼り付ける必要があります。 – Stimul8d

+0

vbまたはvbaですか? – Tipx

答えて

0

だから、曲の全体のパスから曲名のみを抽出したいとします。単純なトリッキーなロジックがこれらを実行します。曲の情報を保持できるオブジェクトを作成します
1:私はそれをこのような何かをするだろう

Right Func, cuts the right part of the string into sub-string of given number 
Len Func, To find the length of the string 
InStrRev Fun, gives the point of occurrence, of the given character in a string 
searching from right to left 
Left Func, cuts the Left part of the string into sub-string of given number 
0

:これは、VBA

sub song_name_extractor() 
    song_path = "c:\songs\favret\mylove.mp3" ' Assume this is where the song is 
    song_name = Right(song_path, Len(song_path) - InStrRev(song_path, "\")) 
    Msgbox song_name 'outputs mylove.mp3 
    song_name = Left(song_name, InStrRev(song_name, ".") - 1) 
    Msgbox song_name ' outputs only mylove removes extensions of file 
end sub 

、について説明します。
2.プレイリスト内のすべての曲を保持するリストを作成します。
3.リストをデータソースとしてリストボックスに追加します.DisplayMemberを設定すると、プロパティがリストボックスのlistitemtextとして表示されます。
4. listindexが変更されたら、listbox.SelectedItemに格納されているオブジェクトを取得し、それをソングオブジェクトにコピーして操作します。

Public Class Form1 

Structure SongObject 
    Public SongPath As String 
    Public NameNoExtension As String 
    Public SongLength As Integer 
    Public SongRating As Integer 
    Private _FileName 
    Public Property FileName() As String 
     Get 
      Return _filename 
     End Get 
     Set(ByVal value As String) 
      _FileName = value 
     End Set 
    End Property 

    Public Sub New(ByVal path As String) 
     SongPath = path 
     FileName = IO.Path.GetFileName(path) 
     NameNoExtension = IO.Path.GetFileNameWithoutExtension(path) 
     SongLength = 0 'fix 
     SongRating = 5 'fix 
    End Sub 

End Structure 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Dim musicpath As String = "C:\Users\Public\Music\Sample Music\" 

    'Load songs into listbox 
    ListBox1.DisplayMember = "FileName" 
    Dim songs As New List(Of SongObject) 
    For Each f As String In IO.Directory.GetFiles(musicpath, "*.mp3") 
     songs.Add(New SongObject(f)) 
    Next 
    ListBox1.DataSource = songs 
End Sub 

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged 
    'Get songitem 
    Dim SelectedSong As SongObject = CType(ListBox1.SelectedItem, SongObject) 
    MsgBox("Path:" & SelectedSong.SongPath & vbCrLf & "FileName:" & SelectedSong.FileName) 
    'Todo: play selected song... 
End Sub 
End Class 

使用IO.Path.GetFileName(パス)とIO.Path.GetFileNameWithoutExtension(パス)ファイル名の代わりに/左/右INSTR /ミッドなどを取得します。

関連する問題