5 Replies - 3076 Views - Last Post: 01 November 2012 - 04:20 PM Rate Topic: -----

#1 Flintvic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 12

How to add random names from ListBox into array?

Posted 29 October 2012 - 01:41 PM

I have a program where I am supposed to code in student names inside a listbox and then randomly add those elements from the listbox into 4 arrays then display each array to its own label box. I am currently stuck at the randomizing the elements and adding them to an array.

Public Class Form1
    Dim strGrp1(4) As String
    Dim strGrp2(4) As String
    Dim strGrp3(4) As String
    Dim strGrp4(5) As String
    Dim strStudent As String
    Dim intShow As Integer

    
    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click


        If txtStdnt.Text = "" Then
            MessageBox.Show("Please enter a Student's Name")
            txtStdnt.Focus()
        Else
            lstNames.Items.Add(txtStdnt.Text)
            txtStdnt.Clear()
            txtStdnt.Focus()
        End If
    End Sub

    Public Sub btnGrp_Click(sender As System.Object, e As System.EventArgs) Handles btnGrp.Click
        Dim Rnd As New Random
        Dim i As Integer = lstNames.Items.Count - 1
        Dim intGroup As Integer = 1


        Do Until lstNames.Items.Count = 0
            strStudent = Rnd.Next(lstNames.Items.Count - 1)
            If intGroup = 1 Then
                strGrp1(i) = strStudent
                intGroup += 1
            ElseIf intGroup = 2 Then
                strGrp2(i) = strStudent
                intGroup += 1
            ElseIf intGroup = 3 Then
                strGrp3(i) = strStudent
                intGroup += 1
            ElseIf intGroup = 4 Then
                strGrp4(i) = strStudent
                intGroup += 1
            End If
        Loop

        For Each item In strGrp1
            lblGrp1.Text = item & vbCrLf
        Next


    End Sub


End Class



Is This A Good Question/Topic? 0
  • +

Replies To: How to add random names from ListBox into array?

#2 Flintvic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 12

Re: How to add random names from ListBox into array?

Posted 29 October 2012 - 02:21 PM

I've got it to randomly add elements from the listbox to the array using this code but when it gets to the last set of elements I run into an error. " InvalidArgument=Value of '2' is not valid for 'index'."


       
 Dim Rnd As New Random
 Dim intGroup As Integer = 1


        For i As Integer = 0 To lstNames.Items.Count
            strStudent = Rnd.Next(lstNames.Items.Count - 1)
            If intGroup = 1 Then
                strGrp1(i) = strStudent
                intGroup += 1
                lstNames.Items.RemoveAt(i)
            ElseIf intGroup = 2 Then
                strGrp2(i) = strStudent
                intGroup += 1
                lstNames.Items.RemoveAt(i)
            ElseIf intGroup = 3 Then
                strGrp3(i) = strStudent
                intGroup += 1
                lstNames.Items.RemoveAt(i)
            ElseIf intGroup = 4 Then
                strGrp4(i) = strStudent
                intGroup += 1
                lstNames.Items.RemoveAt(i)
            End If

Was This Post Helpful? 0
  • +
  • -

#3 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How to add random names from ListBox into array?

Posted 29 October 2012 - 05:53 PM

Quote

      
Dim Rnd As New Random
Dim intGroup As Integer = 1


       For i As Integer = 0 To lstNames.Items.Count
           strStudent = Rnd.Next(lstNames.Items.Count - 1)
           If intGroup = 1 Then
               strGrp1(i) = strStudent
               intGroup += 1
               lstNames.Items.RemoveAt(i)
           ElseIf intGroup = 2 Then
               strGrp2(i) = strStudent
               intGroup += 1
               lstNames.Items.RemoveAt(i)
           ElseIf intGroup = 3 Then
               strGrp3(i) = strStudent
               intGroup += 1
               lstNames.Items.RemoveAt(i)
           ElseIf intGroup = 4 Then
               strGrp4(i) = strStudent
               intGroup += 1
               lstNames.Items.RemoveAt(i)
           End If


Well, for starters, you are using i as a counter to count from 0 to the number of entries in the ListBox, but then during the loop, you are removing entries, so the result is going to be that sooner or later, lstNames.Items.Count, which is resolved at the time the For is executedwill fall to the value of i, at which time, you will get a value that is out of range.

Additionally, you are removing items from the ListBox, but I don't think you want to remove the ones you are removing. During the first iteration of the For loop, you are removing the first entry with lstNames.Items.RemoveAt(i), which removes Item(0). The next time through, you are removing Item(1), which is the third item in the original list.

You should also have a look at your statement strStudent = Rnd.Next(lstNames.Items.Count - 1). It generates a random number betwee 0 and lstNames.Items.Count - 2 inclusive. Then it assigns that number as a string, to strStudent. In other words, that doesn't produce a student name.

Your loop, as it stands, is only good for 4 student names. It will not do anything after intGroup exceeds 4.

I reommend setting a breakpoint on the For statement, and when it stops, step through one step at a time (Debug Menu in Visual Studio), checking the valuses and looking at what happens.

You now have enough to help you progress, so if you run into further problems, just let us know.
Was This Post Helpful? 0
  • +
  • -

#4 Flintvic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 12

Re: How to add random names from ListBox into array?

Posted 30 October 2012 - 08:41 PM

View Postlar3ry, on 29 October 2012 - 05:53 PM, said:

You should also have a look at your statement strStudent = Rnd.Next(lstNames.Items.Count - 1). It generates a random number betwee 0 and lstNames.Items.Count - 2 inclusive. Then it assigns that number as a string, to strStudent. In other words, that doesn't produce a student name.


strStudent = lstNames.Items.Item(Rnd.Next(i)).ToString
like that? That got it to display a random element but still having problem on how to remove that specific element.
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: How to add random names from ListBox into array?

Posted 30 October 2012 - 10:09 PM

View PostFlintvic, on 30 October 2012 - 09:41 PM, said:

strStudent = lstNames.Items.Item(Rnd.Next(i)).ToString
like that? That got it to display a random element but still having problem on how to remove that specific element.


Yes, but you've generated a random number and used it, and now it's gone. What you really need to do is to generate a random number, save it in a variable, use it to assign that entry to wherever you want it, then use it to remove the entry. Think about where to generate it, bearing in mind that each time through the loop, the ListBox.Count will get shorter.

This post has been edited by lar3ry: 30 October 2012 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

#6 Flintvic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 29-October 12

Re: How to add random names from ListBox into array?

Posted 01 November 2012 - 04:20 PM

Thank you larry for your help and the storing the random statement into a variable. My professor helped me with the rest. I will paste the code for others in the future in my same position.
Public Class Teacher
    'Declaring 4 string arrays and accumulator
    Dim strGrp1(-1) As String
    Dim strGrp2(-1) As String
    Dim strGrp3(-1) As String
    Dim strGrp4(-1) As String
    Dim intShow As Integer ' accumulator


    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        'validates then input entries into the listbox
        If txtStdnt.Text = "" Then
            MessageBox.Show("Please enter a Student's Name")
            txtStdnt.Focus()
        Else
            lstNames.Items.Add(txtStdnt.Text)
            txtStdnt.Clear()
            txtStdnt.Focus()
        End If

        'stores element count for display in a label
        lblShow.Text = lstNames.Items.Count
    End Sub

    Private Sub btnGrp_Click(sender As System.Object, e As System.EventArgs) Handles btnGrp.Click
        ' declare a random integers
        Dim Rnd As New Random
        Dim intGroup As Integer = 1
        Dim r As String
        Dim strStudent As String

        'loops throws a random name into an array and increment until empty
        For i As Integer = 0 To lstNames.Items.Count - 1
            r = Rnd.Next(0, lstNames.Items.Count)
            strStudent = lstNames.Items(r)
            lstNames.Items.RemoveAt(r)
            If intGroup = 1 Then
                ReDim Preserve strGrp1(strGrp1.Length)
                strGrp1(strGrp1.Length - 1) = strStudent
                intGroup += 1
            ElseIf intGroup = 2 Then
                ReDim Preserve strGrp2(strGrp2.Length)
                strGrp2(strGrp2.Length - 1) = strStudent
                intGroup += 1
            ElseIf intGroup = 3 Then
                ReDim Preserve strGrp3(strGrp3.Length)
                strGrp3(strGrp3.Length - 1) = strStudent
                intGroup += 1
            ElseIf intGroup = 4 Then
                ReDim Preserve strGrp4(strGrp4.Length)
                strGrp4(strGrp4.Length - 1) = strStudent
                intGroup = 1
            End If
        Next
        Call Display(strGrp1, strGrp2, strGrp3, strGrp4)

    End Sub

    Private Sub Display(ByVal strGrp1() As String, ByVal strGrp2() As String, ByVal strGrp3() As String, ByVal strGrp4() As String)
        'Clears text boxes
        lblGrp1.Text = String.Empty
        lblGrp2.Text = String.Empty
        lblGrp3.Text = String.Empty
        lblGrp4.Text = String.Empty

        'displays the random placed elements in the arrays into labels
        For i As Integer = 0 To strGrp1.Length - 1
            lblGrp1.Text &= strGrp1(i) & vbCrLf
        Next
        For i As Integer = 0 To strGrp2.Length - 1
            lblGrp2.Text &= strGrp2(i) & vbCrLf
        Next
        For i As Integer = 0 To strGrp3.Length - 1
            lblGrp3.Text &= strGrp3(i) & vbCrLf
        Next
        For i As Integer = 0 To strGrp4.Length - 1
            lblGrp4.Text &= strGrp4(i) & vbCrLf
        Next
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'Clears the boxes 
        lblGrp1.Text = String.Empty
        lblGrp2.Text = String.Empty
        lblGrp3.Text = String.Empty
        lblGrp4.Text = String.Empty
        txtStdnt.Clear()
        lstNames.Items.Clear()
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1