Randomize items in listbox?

Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
3 posts Page 1 of 1
Contributors
User avatar
skinke5000
Just Registered
Just Registered
Posts: 6
Joined: Mon Feb 01, 2010 11:17 pm

Randomize items in listbox?
skinke5000
Does anybody know some code to randomize the order of the items in a listbox?
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4392
Joined: Tue Aug 04, 2009 1:47 am

Re: Randomize items in listbox?
CodenStuff
Hello skinke5000,

This code can randomize them nicely:
Code: Select all
 Dim Rand As New System.Random
        'SAVE ARRAYLIST OF ALL ITEMS FROM LISTBOX
        Dim ListBoxItems As New System.Collections.ArrayList(ListBox1.Items)
        'REMOVE ALL ITEMS FROM LISTBOX
        ListBox1.Items.Clear()
        'START RE POPULATING LISTBOX
        While ListBoxItems.Count > 0
            Dim Item As System.Int32 = Rand.Next(0, ListBoxItems.Count)
            ListBox1.Items.Add(ListBoxItems(Item))
            ListBoxItems.RemoveAt(Item)
        End While
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
User avatar
skinke5000
Just Registered
Just Registered
Posts: 6
Joined: Mon Feb 01, 2010 11:17 pm

Re: Randomize items in listbox?
skinke5000
Thanks again cooll;
3 posts Page 1 of 1
Return to “Tutorial Requests”