如何从 Visual Basic 中的列表框中选择随机项

How to pick a random item from a listbox in visual Basic

我试过如何从 listBox.This 中随机挑选一件物品。

Public currentTrack As Integer
Public temp As Integer
Public generic As Random = New Random()
temp = gen.Next(0, listTracks.Items.Count + 1)
        currentTrack = temp
        MessageBox.Show(listTracks.Items(currentTrack))
        AxWindowsMediaPlayer1.URL = listTracks.Items(currentTrack)

当我在消息框显示选择的项目时,它弹出 2 条消息 Boxes.Each 消息框显示不同的项目。 示例:第一个消息框显示 C:\test\blab.mp3 第二个显示 C:\test\bleasds.mp3

你可以试试

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim currentTrack As Integer, temp As Integer, gen As Random = New Random()
    Do
        temp = gen.Next(0, listTracks.Items.Count)
        currentTrack = temp
        Dim result1 As DialogResult = MessageBox.Show(listTracks.Items(currentTrack).ToString + vbNewLine + "Play ?",
                                                  "Play", MessageBoxButtons.YesNo)
        If result1 = DialogResult.Yes Then
            AxWindowsMediaPlayer2.URL = listTracks.Items(currentTrack).ToString
        Else
            Exit Do
        End If
    Loop
End Sub