Results 1 to 4 of 4

Thread: Random sort of listBox index?

  1. #1

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Question Random sort of listBox index?

    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;
                    }
                }
            }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Random sort of listBox index?

    vb.net Code:
    1. Private randomiser As New Random
    2.  
    3. Private Sub RandomiseListBox()
    4.     Dim count As Integer = Me.ListBox1.Items.Count
    5.     Dim item As Object
    6.  
    7.     For index As Integer = 0 To count - 2 Step 1
    8.         item = Me.ListBox1.Items(Me.randomiser.Next(index, count))
    9.         Me.ListBox1.Items.Remove(item)
    10.         Me.ListBox1.Items.Insert(index, item)
    11.     Next index
    12. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Random sort of listBox index?

    D'oh! Here's the C# version:
    Code:
    private Random randomiser = new Random();
    
    private void RandomiseListBox()
    {
    	int count = this.listBox1.Items.Count;
    	object item;
    
    	for (int index = 0; index <= count - 2; index++)
    	{
    		item = this.listBox1.Items[this.randomiser.Next(index, count)];
    		this.listBox1.Items.Remove(item);
    		this.listBox1.Items.Insert(index, item);
    	}
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Lively Member Zolomon's Avatar
    Join Date
    Oct 2007
    Location
    Sweden
    Posts
    104

    Re: Random sort of listBox index?

    Ah, thank you!

    The problem is now solved.

    Cheers,
    Zolomon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width