Shuffle listbox items

BlitzPlus Forums/BlitzPlus Programming/Shuffle listbox items

I have tried a million ways to shuffle listbox items, but its done nothing but cause the program to crash. Any ideas or algorithms?

Here you go, a nice little function for you to use! ;)

Local hWnd = CreateWindow("Test",20,20,200,200,Desktop(),3)
Local lbxList = CreateListBox(10,10,100,100,hWnd)

AddGadgetItem(lbxList,"One")
AddGadgetItem(lbxList,"Two")
AddGadgetItem(lbxList,"Three")
AddGadgetItem(lbxList,"Four")

Local btnButton = CreateButton("Shuffle",10,120,100,25,hWnd)


Repeat
	Select WaitEvent(100)
		Case $803 
			End
		Case $401
			Select EventSource()
				Case btnButton
					lbxList = ShuffleListBox(lbxList,hWnd)
			End Select
	End Select 
Forever

Function ShuffleListBox%(hOldListBox,hWindow)
SeedRnd MilliSecs()
Local randomNumber = 0
Local ItemCount = 0
;Hide the old one
HideGadget hOldListBox

;Create a new one and fill it randomly 
hNewListBox = CreateListBox(GadgetX(hOldListBox),GadgetY(hOldListBox),GadgetWidth(hOldListBox),GadgetHeight(hOldListBox),hWindow)

Repeat
	ItemCount = CountGadgetItems(hOldListBox)
	If itemCount = 0 Then Exit 
	randomNumber = Rand(0,ItemCount-1)
	AddGadgetItem(hNewListBox,GadgetItemText$(hOldListBox,randomNumber))
	RemoveGadgetItem(hOldListBox,randomNumber)
Forever 

;I like to free it, but it will be destroyed anyway!
FreeGadget hOldListBox
hOldListBox = 0

Return hNewListBox
End Function


Dabz

Thanks a million!