我正在製作音樂播放器應用程序。我正在使用ListBox來顯示歌曲。當用戶添加歌曲時,它會顯示歌曲的完整路徑。但我希望它只顯示歌曲名稱(歌曲可以位於任何驅動器的任何文件夾中)。 Windows媒體播放器控件正在播放歌曲。提前致謝!在列表框中存儲文件的路徑
0
A
回答
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
釋:
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,您將告訴wich屬性在列表框中將顯示爲listitemtext。
4.當listindex更改時,獲取存儲在listbox.SelectedItem中的對象,並將其輸入到一個songobject以使用它。
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 /中和等。
相關問題
- 1. 在屬性文件中存儲路徑
- 2. 存儲文件路徑
- 3. 在對話框中選擇存儲文件路徑Excel VBA
- 4. 路徑存儲在配置文件?
- 5. 獲取文件的路徑從自動保存列表框
- 6. 不尋常的存儲文件路徑
- 7. 存儲文件夾的路徑
- 8. 存儲文件的路徑和映射
- 9. 更改存儲在TFS中的配置文件中的路徑
- 10. 路徑中的列表文件
- 11. Azure blob存儲文件路徑限制
- 12. 如何修復文件存儲路徑
- 13. django自定義文件存儲路徑
- 14. 存儲文件路徑扔掉「\」
- 15. 將文件路徑存儲到QString [Qt]
- 16. 存儲路徑
- 17. 哪個更好?在DB中存儲文件或在DB中存儲其路徑?
- 18. 存儲在SQL中的文件的網絡路徑?
- 19. 如何設置存儲在資源中的文件的路徑?
- 20. 將文件路徑轉換爲TreeUri(存儲訪問框架)
- 21. HTML5文件和文件列表路徑
- 22. 循環通過路徑列表保存在文本文件
- 23. 無法在外部存儲中創建新的文件路徑
- 24. 在SD卡中存儲文件的確切路徑是什麼
- 25. 刪除存儲在一個文件中FULLPATH的路徑前綴
- 26. 如何在Blackberry JDE中指定存儲文件的路徑?
- 27. 在asp.net中獲取並存儲文件的路徑5
- 28. 存儲在數據庫中的更新文件路徑
- 29. 在數據庫中存儲上傳文件的路徑
- 30. 使用PHP在SQL數據庫中存儲文件的路徑
您需要說明您使用的是哪個演示文稿框架Sadia。這聽起來像它可能是WPF/Silverlight,但它可能是Winforms或其他。此外,您需要粘貼一些代碼以便人們更輕鬆地幫助。 – Stimul8d
它是vb還是vba? – Tipx