You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 song array to ListBox1 within the loop
  • Your code was missing the Next statement to close the For loop, plus the End If for 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:23:42