Hello!
I am currently trying to implement an option to sort a listBox's (lbImages) items in a random way. Each item of the listBox represent a string, and I wish for these to occur, everytime it's called, in a new and random order.
How can I accomplish this?
Here is my current code, but I just can't get it to work right:
Code:private void cbxSort_SelectedIndexChanged(object sender, EventArgs e) { if (cbxSort.Text == "Alphanumerical") { lbImages.Sorted = true; } else if (cbxSort.Text == "Random") { Random random = new Random(); List<string> randomList = new List<string>(); for (int i = 0; i < lbImages.Items.Count; i++) { randomList.Add(lbImages.Items.IndexOf(i).ToString()); } lbImages.Items.Clear(); for (int i = randomList.Count; i > 0; i--) { iNewRandomIndex = random.Next(0, randomList.Count - 1); lbImages.Items.Add(randomList.IndexOf(i)); // Have a problem in this line randomList.RemoveAt(iNewRandomIndex); iLastRandomIndex = iNewRandomIndex; } } }




Reply With Quote