Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRandomize the values in a series of TextBoxes, a ListBox, or a single TextBox in Visual Basic 6
DescriptionThis example shows how to randomize the values in a series of TextBoxes, a ListBox, or a single TextBox in Visual Basic 6.
Keywordsrandomize, TextBox, ListBox, Visual Basic 6
CategoriesAlgorithms
 
The RandomizeArray subroutine randomizes an array of integers. For each position j in the array, it randomly selects an item that has not yet been positioned and swaps it into position j.
 
' Randomize an array of numbers.
Public Sub RandomizeArray(items() As Integer)
Dim min_item As Integer
Dim max_item As Integer
Dim i As Integer
Dim j As Integer
Dim tmp_value As Integer

    min_item = LBound(items)
    max_item = UBound(items)
    
    For i = min_item To max_item - 1
        ' Randomly assign item number i.
        j = Int((max_item - i + 1) * Rnd + i)
        tmp_value = items(i)
        items(i) = items(j)
        items(j) = tmp_value
    Next i
End Sub
 
To randomize the values in a group of TextBoxes, the program loops through the TextBoxes in a control array and copies their values into an array.

It then calls RandomizeArray to randomize the values and copies the randomizes values back into the TextBoxes.

 
' Randomize the numbers in the TextBoxes.
Private Sub cmdRandomizeTextBoxes_Click()
Dim values() As Integer
Dim i As Integer

    ' Load the valeus into an array.
    ReDim values(txtNumber.LBound To txtNumber.UBound)
    For i = txtNumber.LBound To txtNumber.UBound
        values(i) = Val(txtNumber(i).Text)
    Next i

    ' Randomize.
    RandomizeArray values

    ' Display the results.
    For i = txtNumber.LBound To txtNumber.UBound
        txtNumber(i).Text = values(i)
    Next i
End Sub
 
To randomize the values in a ListBox, the program loops through the ListBox values and copies them into an array.

It then calls RandomizeArray to randomize the values and copies the randomizes values back into the ListBox.

 
' Randomize the ListBox.
Private Sub cmdRandomizeListBox_Click()
Dim i As Integer
Dim values() As Integer

    ' Get the values.
    ReDim values(0 To lstNumbers.ListCount - 1)
    For i = 0 To lstNumbers.ListCount - 1
        values(i) = lstNumbers.List(i)
    Next i

    ' Randomize.
    RandomizeArray values

    ' Display the results.
    lstNumbers.Clear
    For i = LBound(values) To UBound(values)
        lstNumbers.AddItem values(i)
    Next i
End Sub
 
To randomize the values in a single TextBox, the program uses Split to split the values separated by commas apart. It loops through the values and copies them into an array.

It then calls RandomizeArray to randomize the values and copies the randomizes values back into the TextBox.

 
' Randomize the numbers in the single TextBox.
Private Sub cmdRandomizeTextBox_Click()
Dim numbers() As String
Dim i As Integer
Dim values() As Integer
Dim txt As String

    ' Get the values.
    numbers = Split(txtNumbers.Text, ",")
    ReDim values(LBound(numbers) To UBound(numbers))
    For i = LBound(numbers) To UBound(numbers)
        values(i) = Val(numbers(i))
    Next i

    ' Randomize.
    RandomizeArray values

    ' Display the results.
    txt = ""
    For i = LBound(values) To UBound(values)
        txt = txt & ", " & values(i)
    Next i
    If Len(txt) > 0 Then txt = Mid$(txt, 2)
    txtNumbers.Text = txt
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated