2011-10-23 2 views
0

음악 플레이어 응용 프로그램을 만들고 있습니다. ListBox를 사용하여 노래를 표시합니다. 사용자가 노래를 추가하면 노래의 전체 경로가 표시됩니다. 하지만 노래 이름 만 표시하고 싶습니다 (노래는 모든 드라이브의 폴더에있을 수 있음). Windows Media Player 컨트롤이 노래를 재생합니다. 미리 감사드립니다!목록 상자에 파일 경로 저장

+0

여기서 Sadia를 사용하고있는 프레젠테이션 프레임 워크를 말해야합니다. WPF/Silverlight 일지 모르지만 Winforms 또는 다른 것일 수 있습니다. 또한 좀 더 쉽게 도움을 줄 수있는 코드를 붙여 넣을 필요가 있습니다. – Stimul8d

+0

vb 또는 vba입니까? – Tipx

답변

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 

Explaination :

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

나는 이런 식으로 뭔가를 할 것이다 :
1. 노래의 정보를 저장할 수있는 객체를 생성합니다.
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/미드 등을 얻을 수 있습니다.

관련 문제