Results 1 to 3 of 3

Thread: Howto randomize List box items? (please)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84

    Question


    I'm almost on the verge of figuring this out ... but, I could use some help (please). My brain is fried ... this is the extent of my Basic thinking abilities (kinda sad I guess)

    Thanks for any help!

    Dim ListCount As Integer
    Dim Store_Old_List()
    Dim Store_New_List_Count()
    Dim MyRandom As Integer

    ListCount = Listbox.ListCount

    For X = 1 to ListCount
    Store_Old_List(X) = Listbox.list(x-1)
    Next
    'Old list is stored

    For Y = 1 to ListCount
    Loop:
    Randomize
    MyRandom = Int((ListCount * Rnd) + 1)
    For X = 1 to Y
    If MyRandom = New_List_Count(X) Then
    Goto Loop 'random number already stored
    End If
    Next
    New_List_Count(Y) = MyRandom
    Next

    For X = 1 To ListCount
    ListBox.RemoveItem 0
    Next 'Clear the listbox

    For X = 1 To ListCount
    Listbox.additem Store_Old_list(New_List_Count(X))
    Next

    The_End:



  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2000
    Posts
    84
    Great balls of fire! I actually figured it out on my own! (someone pat me on the back)


    ------------------------
    Private Sub Random_Click()
    Dim ListCount As Integer
    Dim Store_Old_List()
    Dim Store_New_List_Count()
    Dim MyRandom As Integer

    ListCount = SelectedMaps.ListCount

    ReDim Store_Old_List(ListCount)
    ReDim Store_New_List_Count(ListCount)

    For X = 1 To ListCount
    Store_Old_List(X) = SelectedMaps.List(X - 1)
    Next
    'Old list is stored

    For Y = 1 To ListCount
    My_Loop:
    Randomize
    MyRandom = Int((ListCount * Rnd) + 1)
    For X = 1 To Y
    If MyRandom = Store_New_List_Count(X) Then
    GoTo My_Loop 'random number already stored
    End If
    Next
    Store_New_List_Count(Y) = MyRandom
    Next

    For X = 1 To ListCount
    SelectedMaps.RemoveItem 0
    Next 'Clear the listbox

    For X = 1 To ListCount
    SelectedMaps.AddItem Store_Old_List(Store_New_List_Count(X))
    Next

    End Sub
    -------------------

  3. #3
    Hyperactive Member
    Join Date
    Aug 2000
    Posts
    258
    The following will pull a random item from a list box . I see that you don't need it but I thought I'd put it here anyway .
    [code]
    Public Function listbox_RandomItem(lst As ListBox) As String
    '***********************************
    ' - Nov 4 , 2000
    'Get a random item from the listbox
    '***********************************
    Dim lst

    Dim upper, lower, MyValue As Integer
    upper = lst.ListCount - 1
    lower = 0

    Randomize
    MyValue = Int((upper * Rnd) + 1) ' Generate random value between 1 and Highest index of lst.
    lst.Selected(MyValue) = True

    If lst.Text <> "" Then
    lstbox_RandomItem = lst.Text
    End If

    End Function
    [code]

    Code:
    retval = listbox_RandomItem(list1)

    []P
    Visual Basic 6 SP4 on win98se

    QUIT THE RAT RACE BECAUSE YOUR MESSING THE WORLD UP !!!!!

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