我正在尝试使用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覆盖)
请帮帮我!
答案 0 :(得分:0)
如果使用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