I am making a very simple MP3, WAV, and WMA media player.
The music files are listed within a listView consisting of two columns (Column 1: Audio Title, Column 2: File Location).
I would like to create a button that will shuffle and randomize ALL of the items that are in the list view.
Here's an example:
Title Location
SONG1 C:\\A LOCATION
SONG2 "
SONG3 "
SONG4 "
SONG5 "
SONG6 "
into this:
Title Location
SONG6 C:\\A LOCATION
SONG3 "
SONG4 "
SONG2 "
SONG1 "
SONG5 "
I'm using axMediaPlayer (.wmp)
Thank you for the help! :)
Random rnd = new Random();
var randomizedList = from item in listbox.Items
orderby rnd.Next()
select item;
Then assign the randomizedList back to the listbox
or
private static Random rng = new Random();
public static void Shuffle<T>(this IList<T> list)
{
int n = list.Count;
while (n > 1) {
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
Usage:
var products = listview.Items.ToList();
products.Shuffle();
using System;
using System.Collections;
using System.Windows.Forms;
public static class ListViewExtensions
{
public static void Randomize(this ListView lv)
{
ListView.ListViewItemCollection list = lv.Items;
Random rng = new Random();
int n = list.Count;
lv.BeginUpdate();
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
ListViewItem value1 = (ListViewItem)list[k];
ListViewItem value2 = (ListViewItem)list[n];
list[k] = new ListViewItem();
list[n] = new ListViewItem();
list[k] = value2;
list[n] = value1;
}
lv.EndUpdate();
lv.Invalidate();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.