如何在ListBox中堆叠MP3文件?AxWindowsMediaPlayer播放器导入代码求助
Fixing ListBox Item Addition for Your AxWindowsMediaPlayer Music Player
Hey there! Let's sort out that missing piece in your code for adding imported songs to ListBox1. Looking at your snippet, you've already got the core parts—grabbing filenames and full paths via OpenFileDialog—but you just need to finish the loop and add a couple of small touches to make it work properly.
Here's what's missing and how to fix it:
- You need to explicitly add each song name from the
songarray to ListBox1 within the loop - Your code was missing the
Nextstatement to close the For loop, plus theEnd Iffor the OpenFileDialog check - It's a good practice to clear the ListBox first before adding new items, so you don't get duplicate entries if you select files multiple times
Here's the corrected complete code:
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 ' Clear existing items to avoid duplicate entries ListBox1.Items.Clear() ' Add each song name to the ListBox For items As Integer = 0 To song.Count - 1 ListBox1.Items.Add(song(items)) Next ' Close the For loop End If ' Close the OpenFileDialog check Catch ex As Exception ' Optional: Add error handling to catch unexpected issues MessageBox.Show($"Error importing files: {ex.Message}") End Try End Sub End Class
A quick tip for later playback: When you want to play a selected song from the ListBox, you can use the selected index to pull the full file path from the directory array. For example:
If ListBox1.SelectedIndex <> -1 Then AxWindowsMediaPlayer1.URL = directory(ListBox1.SelectedIndex) End If
内容的提问来源于stack exchange,提问作者Dolan Duccer
相关产品推荐
相关产品推荐

