I got the textbox and a listbox and a button, I can add names threw the textbox hit the button to add and it adds to the listbox, now I need to take the names from there and randomize them into 4 groups and make it into an array also, I just dont know how to take the names I entered and randomize them and put them into 4 different groups without repeating themselves, it is ok if three groups that have 5 and the fourth has 6. They just need to be randomized and put into 4 different groups, can someone help me.
This is what I have, but I think its the wrong way to do this, to make the code work right atleast

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add(TextBox1.Text)
    End Sub


End Class

Recommended Answers

All 5 Replies

See if this helps.
5 ListBoxes(items in ListBox1)

Public Class Form1
    Private arlGroup1, arlGroup2, arlGroup3, arlGroup4 As New ArrayList '// 4 ArrayLists to store random items.
    Private lstIndexes As New List(Of Integer) '// List to add random indexes to.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        lstIndexes.Clear()
        Dim rnd As New Random '// for Randomizing.
        Dim iTemp As Integer = 0 '// stores a random item index.
        Do While Not lstIndexes.Count = ListBox1.Items.Count '// loop until your List of Integer equals your ListBox in .Count.
            iTemp = rnd.Next(0, ListBox1.Items.Count) '// get a random item index.
            If Not lstIndexes.Contains(iTemp) Then lstIndexes.Add(iTemp) '// if not in List of Integer, add index of item.
        Loop
        '// Clear all ArrayLists.
        arlGroup1.Clear() : arlGroup2.Clear() : arlGroup3.Clear() : arlGroup4.Clear()
        '// loop thru all stored Indexes and add item by index from ListBox.
        For i As Integer = 0 To lstIndexes.Count - 1 Step 4 '// Step 4 skips to every 5 item.
            arlGroup1.Add(ListBox1.Items(lstIndexes(i)))
            '// check if i +1 does not equal or is greater than the items.Count in your List of Integer, Else Exit the loop.
            If Not lstIndexes.Count <= i + 1 Then arlGroup2.Add(ListBox1.Items(lstIndexes(i + 1))) Else Exit For
            arlGroup3.Add(ListBox1.Items(lstIndexes(i + 2)))
            arlGroup4.Add(ListBox1.Items(lstIndexes(i + 3)))
        Next
        '//======= FOR TESTING ==========
        '// Clear ListBoxes.
        ListBox2.Items.Clear() : ListBox3.Items.Clear() : ListBox4.Items.Clear() : ListBox5.Items.Clear()
        '// add items from ArrayLists.
        ListBox2.Items.AddRange(arlGroup1.ToArray)
        ListBox3.Items.AddRange(arlGroup2.ToArray)
        ListBox4.Items.AddRange(arlGroup3.ToArray)
        ListBox5.Items.AddRange(arlGroup4.ToArray) '=======\\
    End Sub
End Class

thank you this should help me out but im lil confused on how many buttons, listboxes, textboxes etc. that I need to use. Especially when it says there are list box2 - 5 on there, do I need to add 5 listboxes or will those automatically come up on its own?

>to take the names from there and randomize them into 4 groups and make it into an array also,...

I added 4 ArrayLists which store your ListBox1 items randomly. The ListBoxes 2-5 are there for '//======= FOR TESTING ========== , unless you need them otherwise.

If you just need the items from each Group ArrayList, use MsgBox(arlGroup1(0)) to get the first item in the ArrayList for Group1, MsgBox(arlGroup1(1)) to get the second item, and so on.

Overall, you only need 1 ListBox (ListBox1) to have items in, and delete all the code just following the line of '//======= FOR TESTING ========== .

The For/Next loop did not crash when I tested, but has crashed on others.
To not have it crash, since this is due the the items.Count in ListBox1, use this update for the For/Next loop.

For i As Integer = 0 To lstIndexes.Count - 1 Step 4 '// Step 4 skips to every 5 item.
            arlGroup1.Add(ListBox1.Items(lstIndexes(i)))
            '// check if i +1 does not equal or is greater than the items.Count in your List of Integer, Else Exit the loop.
            If Not lstIndexes.Count <= i + 1 Then arlGroup2.Add(ListBox1.Items(lstIndexes(i + 1))) Else Exit For
            If Not lstIndexes.Count <= i + 2 Then arlGroup3.Add(ListBox1.Items(lstIndexes(i + 2))) Else Exit For
            If Not lstIndexes.Count <= i + 3 Then arlGroup4.Add(ListBox1.Items(lstIndexes(i + 3))) Else Exit For
        Next

I have to thank codeorder for helping me, this is the code that I needed to get the application to work

Public Class Form1
    Private arlGroup1, arlGroup2, arlGroup3, arlGroup4 As New ArrayList ' 4 ArrayLists to store random items
    Private lstIndexes As New List(Of Integer) ' List to add random indexes to

   

    ' Add students to ListBox
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = "" Then
            ListBox1.Items.Add(TextBox1.Text)
            TextBox1.SelectAll()
        End If
    End Sub

    ' get Random students
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        lstIndexes.Clear()
        Dim rnd As New Random ' for Randomizing
        Dim iTemp As Integer = 0 ' stores random student index
        Do While Not lstIndexes.Count = ListBox1.Items.Count ' loop until your List of Integer equals your ListBox 
            iTemp = rnd.Next(0, ListBox1.Items.Count) ' get a random student index
            If Not lstIndexes.Contains(iTemp) Then lstIndexes.Add(iTemp)
        Loop
        ' Clear all ArrayLists
        arlGroup1.Clear() : arlGroup2.Clear() : arlGroup3.Clear() : arlGroup4.Clear()
        ' loop thru all stored Indexes and add item by index from ListBox
        For i As Integer = 0 To lstIndexes.Count - 1 Step 4 ' Step 4 skips to every 5 item
            arlGroup1.Add(ListBox1.Items(lstIndexes(i)))
            If Not lstIndexes.Count <= i + 1 Then arlGroup2.Add(ListBox1.Items(lstIndexes(i + 1)))
            If Not lstIndexes.Count <= i + 2 Then arlGroup3.Add(ListBox1.Items(lstIndexes(i + 2)))
            If Not lstIndexes.Count <= i + 3 Then arlGroup4.Add(ListBox1.Items(lstIndexes(i + 3)))
        Next

        ' Add random students to Labels  
        If Not arlGroup1.Count = 0 Then ' check if not empty
            Label1.Text = "Group 1: "
            For Each itm As String In arlGroup1 ' loop thru all random students for the Group
                Label1.Text &= itm & ", "
            Next
        End If
        If Not arlGroup2.Count = 0 Then ' check if not empty
            Label2.Text = "Group 2: "
            For Each itm As String In arlGroup2 ' loop thru all random students for the Group
                Label2.Text &= itm & ", "
            Next
        End If
        If Not arlGroup3.Count = 0 Then ' check if not empty
            Label3.Text = "Group 3: "
            For Each itm As String In arlGroup3 ' loop thru all random students for the Group
                Label3.Text &= itm & ", "
            Next
        End If
        If Not arlGroup4.Count = 0 Then ' check if not empty.
            Label4.Text = "Group 4: "
            For Each itm As String In arlGroup4 ' loop thru all random students for the Group
                Label4.Text &= itm & ", "
            Next
        End If

    End Sub

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then Button1_Click(sender, e)
    End Sub
End Class

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.