locked
How do you shuffle items in three different list boxes. RRS feed

  • Question

  • I have a code that shuffles only one listbox but i want it to shuffle all three. How do i do that?


        Private Sub ShuffleList(ByVal listBox1 As ListBox)
            Dim items As New List(Of String)
            For Each item In listBox1.Items
                items.Add(item)
            Next

            listBox1.Items.Clear()
            Dim count As Integer = items.Count
            For i As Integer = 0 To count - 1
                Dim index As Integer = rnd.[Next](0, count - i)
                listBox1.Items.Add(items(index))
                items.RemoveAt(index)
            Next
        End Sub

    Wednesday, December 13, 2017 2:32 PM

All replies

  • Call the above Sub for each ListBox (updated code):

    ShuffleList(ListBox1)
    ShuffleList(ListBox2)
    ShuffleList(ListBox3)
    
        Private Sub ShuffleList(ByVal Listbox As System.Windows.Forms.ListBox)
    
            Dim Random As New System.Random
    
            Listbox.BeginUpdate()
    
            Dim ArrayList As New System.Collections.ArrayList(Listbox.Items)
    
            Listbox.Items.Clear()
    
            While ArrayList.Count > 0
    
                Dim Index As System.Int32 = Random.Next(0, ArrayList.Count)
    
                Listbox.Items.Add(ArrayList(Index))
    
                ArrayList.RemoveAt(Index)
    
            End While
    
            Listbox.EndUpdate()
    
        End Sub



    Paul ~~~~ Microsoft MVP (Visual Basic)


    • Edited by Paul P Clement IV Wednesday, December 13, 2017 3:04 PM Updated code
    • Proposed as answer by Venkat786 Wednesday, December 13, 2017 3:07 PM
    Wednesday, December 13, 2017 2:51 PM
  • I have a code that shuffles only one listbox but i want it to shuffle all three. How do i do that?


        Private Sub ShuffleList(ByVal listBox1 As ListBox)
            Dim items As New List(Of String)
            For Each item In listBox1.Items
                items.Add(item)
            Next

            listBox1.Items.Clear()
            Dim count As Integer = items.Count
            For i As Integer = 0 To count - 1
                Dim index As Integer = rnd.[Next](0, count - i)
                listBox1.Items.Add(items(index))
                items.RemoveAt(index)
            Next
        End Sub

    Hi

    An alternative way to do it.

    ' Form1 with 3 x ListBoxes
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim r As New Random
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' some test data
            ListBox1.Items.AddRange({"1", "2", "3", "4", "5", "6"})
            ListBox2.Items.AddRange({"one", "two", "three", "four", "five", "six"})
            ListBox3.Items.AddRange({"A", "B", "C", "D", "E", "F"})
    
    
            ' shuffle all ListBox contents where
            ' sll listboxes are on parent form
    
            For Each c As Control In Controls
                If c.GetType() Is GetType(ListBox) Then
                    ShuffleList(DirectCast(c, ListBox))
                End If
            Next
    
        End Sub
        Private Sub ShuffleList(ByVal listBox As ListBox)
            Dim lst As ListBox.ObjectCollection = listBox.Items
            Dim c As Integer = lst.Count
            Dim lbi As String
            Dim h, j As Integer
    
            ' loop for 3 times listbox item count
            For i As Integer = 0 To c * 3
                h = r.Next(0, c)
                lbi = lst(h).ToString
                Do
                    j = r.Next(0, c)
                Loop Until Not h = j
                lst(h) = lst(j)
                lst(j) = lbi
            Next
        End Sub
    End Class


    Regards Les, Livingston, Scotland

    Wednesday, December 13, 2017 3:05 PM