如何在列表框中堆叠 mp3 文件

How to stack mp3 files in listbox

我正在尝试使用 AxWindowsMediaPlayer 制作音乐播放器,但我在这个导入代码上遇到了一个小问题。 (导入的项目转到列表框@listbox1)

Public Class Form1
Dim song As String()
Dim directory As String()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        If (OpenFileDialog1.ShowDialog = DialogResult.OK) Then
            song = OpenFileDialog1.SafeFileNames
            directory = OpenFileDialog1.FileNames
            For items As Integer = 0 To song.Count - 1
                ListBox1.Items.Add(song(items))
            Next
        End If
    Catch ex As Exception
    End Try
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Try
    AxWindowsMediaPlayer1.URL = directory(ListBox1.SelectedIndex)
    AxWindowsMediaPlayer1.Ctlcontrols.play()
End Sub
End Class
Catch ex As Exception
    End Try

但是当尝试重新导入新歌曲(在第一次导入之后)并尝试播放时,新导入的歌曲会覆盖之前的歌曲(例如,oldimportedsong1 被 newimportedsong1 覆盖,oldimportedsong2 被 newimportedsong2 覆盖)

请帮帮我!

如果您使用 List(Of T),它将简化您的代码。否则你将不得不 ReDim 保留你的数组。 您的代码中发生的事情是每次单击按钮时都会覆盖数组。

Private song As New List(Of String)
Private directory As New List(Of String)

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            If OpenFileDialog1.ShowDialog = DialogResult.OK Then
                song.AddRange(OpenFileDialog1.SafeFileNames)
                directory.AddRange(OpenFileDialog1.FileNames)
                ListBox1.DataSource = Nothing
                ListBox1.DataSource = song
            End If
        Catch ex As Exception
            'empty catch boxes are the devil's workshop
            MessageBox.Show(ex.Message)
        End Try
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim i As Integer = ListBox1.SelectedIndex
        Dim path As String = directory(i)
End Sub