none
Adding Students to club using arrays RRS feed

  • Question

  • I'm working on a Visual basic assignment where the user is supposed to be able to add students to a club by selecting them form a list box and then they will be added to another list box there is also a drop down combo box where the user can select which club they are adding them to but if they change the selected club it will clear the list and allow them to add students to that club but If they should change back to a club they already added students to the selected list will already be there. this is what the form looks like
    I cant figure out how to have the list of selected name be save so when the user selects a club again the they will all be there.

    this is a full description of the assignment

    www.chegg.com/homework-help/adding-students-clubs-programming-challenge-5-chapter-5-aske-chapter-8-problem-6pc-solution-9780134400150-exc
    here is my code so far

    Public Class Form1
    
        Private strClubNames() As String = {"Honors", "Golden Arrow", "Computer", "Science"}
    
    
        Private intEnrollSizes(strClubNames.Count) As Integer
    
        Private strEnrollments(0, 0) As String
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            For Each club As String In strClubNames
                cboClubs.Items.Add(club)
            Next
    
            ReDim strEnrollments(strClubNames.Count, lstGeneral.Items.Count)
    
        End Sub
        Function CheckForInsertErrors() As Boolean
            lblStatus.Text = String.Empty
    
    
            If lstGeneral.SelectedIndex = -1 Then
                lblStatus.Text = "Please select a name from the general student list"
                Return False
            End If
    
            If cboClubs.SelectedIndex = -1 Then
                lblStatus.Text = "Please select a club name from the list"
                Return False
            End If
    
    
            Return True
        End Function
        Sub Clearmember()
            Dim selected As String = lstMembers.SelectedItem.ToString
            lstMembers.Items.Remove(selected)
    
    
        End Sub
    
    
    
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    
            If Not CheckForInsertErrors() Then Return
    
            Dim name As String = lstGeneral.SelectedItem.ToString()
            Dim clubIndex As Integer = cboClubs.SelectedIndex
    
    
            If Not lstMembers.Items.Contains(name) Then
                lstMembers.Items.Add(name)
                intEnrollSizes(clubIndex) += 1
                lblCount.Text = lstMembers.Items.Count.ToString() & " members"
            End If
    
            'save the names back into the master array
            For i = 0 To lstMembers.Items.Count - 1
                strEnrollments(clubIndex, i) = lstMembers.Items(i).ToString
            Next
    
    
    
        End Sub
    
        Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    
            Clearmember()
            lblCount.Text = lstMembers.Items.Count.ToString() & " members"
        End Sub
    
        Private Sub cboClubs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClubs.SelectedIndexChanged
    
            lstMembers.Items.Clear()
    
        End Sub
    End Class
    

    Tuesday, May 8, 2018 3:07 AM

All replies

  • I cant figure out how to have the list of selected name be save so when the user selects a club again the they will all be there.

    You should be using classes, and avoiding arrays.  The Collection object in .Net comes in a variety of forms, but it seems a simple List(Of  will be adequate for this task, provided it is a list(Of a suitable class. 

    For instance, a Club class would include the club name, and a collection, such as a List(Of students.   The students might be strings at this stage - changing strings into a class later on, when you realize that you need to store more than the students name, will be easy.

    A list of Club objects, where each object includes a property that is itself a list is the equivalent of a two-dimensional array, but it is much easier to manage, expands as required, and each 'array element' (the Club object) can be handled as a single unit of data.

    Also, your procedure should change so that there is never any need to process the listbox list back into the underlying store.  All manipulation should apply to the underlying store - the collection of members (students) in a Club objcet, in this case - and the list boxes should be used only for displaying the current status of the list (which you do whenever it changes) and for allowing the user to indicate their selection.  Updating the listboxes directly from the collections means there is always an exact one-to-one relationship between the displayed list and the underlying data. When the user selects a new club there is no need to save anything - the underlying data in the collection has already been updated when the changes were made.

    • Edited by Acamar Tuesday, May 8, 2018 4:48 AM fmt
    • Proposed as answer by Cor Ligthert Tuesday, May 8, 2018 9:17 AM
    Tuesday, May 8, 2018 4:40 AM
  • Did you ever make any progress on this? I have the same assignment and am stuck. Arrays are extremely confusing to me. My code currently looks like this and Im unsure of where to go next:

    Public Class Form1
        'Declare variables
       Dim intNumStudents As Integer
        Dim intNumMembers As Integer
        Dim strSelection As String
        Dim strClubs() As String = {"Honors", "Golden Arrow", "Computer", "Gaming"}
        Dim clubIndex As Integer
        Dim strClubMembers(0, 0) As String
     
        Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
            'Counts number of members in the list
           intNumMembers = lstMembers.Items.Count - 1
     
            'Checks to make sure a member name is selected when the remove button is clicked
           If lstMembers.SelectedIndex = -1 Then
                MessageBox.Show("Please select a member to remove")
            ElseIf cbClubs.SelectedIndex = -1 Then
                MessageBox.Show("Please select a club")
            Else
                strSelection = lstMembers.Items(lstMembers.SelectedIndex.ToString) 'Converts select to String
               lstStudentList.Items.Add(strSelection) 'Adds selected string to Student List
               lstMembers.Items.Remove(strSelection) 'Removes selected string from members list
               lblMemberCount.Text = intNumMembers.ToString + " members" 'Updates member count label
           End If
        End Sub
     
        Private Sub btnAddStudent_Click(sender As Object, e As EventArgs) Handles btnAddStudent.Click
     
            'Counts number of members in the list
           intNumMembers = lstMembers.Items.Count + 1
     
            'Checks to make sure a student name is selected when the add button is clicked
           If lstStudentList.SelectedIndex = -1 Then
                MessageBox.Show("Please select a student to add")
            ElseIf cbClubs.SelectedIndex = -1 Then
                MessageBox.Show("Please select a club")
            Else
                strSelection = lstStudentList.Items(lstStudentList.SelectedIndex.ToString) 'Converts selected student to string
               lstMembers.Items.Add(strSelection) 'Adds selected student to Members List
               lblMemberCount.Text = intNumMembers.ToString + " members" 'Updates member count label
     
            End If
        End Sub
     
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each club As String In strClubs
                cbClubs.Items.Add(club)
            Next
        End Sub
    End Class
    

    Thursday, July 19, 2018 11:04 PM
  • Hi

    Tell your teacher that Arrays are not the best way to achieve this objective.

    Read the post above by Acamar to see the best way forward.


    Regards Les, Livingston, Scotland

    Thursday, July 19, 2018 11:10 PM
  • Its not about the best to do it. Its about learning arrays.
    Friday, July 20, 2018 12:30 AM
  • Try this

    I add function to show you what is in main array and

    Public Class Form1

      Private strClubNames() As String = {"Honors", "Golden Arrow", "Computer", "Science"}

      '' added
      Private strStudents() As String = {"Adams, Ben", "Baker, Sally", "Canseco, Juan", "Davis, Sharon", "Etienne, Jean", _
                                         "Gonzalez, Jose", "Johnson, Eric", "Koenig, Johann", " Matsunaga, Akiko", _
                                         "Nakamura, Ken", "Ramirez, Maria"}

      Private intEnrollSizes(strClubNames.Count) As Integer

      Private strEnrollments(0, 0) As String

      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        For Each club As String In strClubNames
          cboClubs.Items.Add(club)
        Next

        'added
        lstGeneral.Items.Clear()
        For Each student As String In strStudents
          lstGeneral.Items.Add(student)
        Next

        '' added  (select first student)
        Try
          lstGeneral.SelectedIndex = 0
        Catch ex As Exception
        End Try

        ReDim strEnrollments(strClubNames.Count, lstGeneral.Items.Count)

      End Sub
      Function CheckForInsertErrors() As Boolean
        lblStatus.Text = String.Empty


        If lstGeneral.SelectedIndex = -1 Then
          lblStatus.Text = "Please select a name from the general student list"
          Return False
        End If

        If cboClubs.SelectedIndex = -1 Then
          lblStatus.Text = "Please select a club name from the list"
          Return False
        End If


        Return True
      End Function
      Sub Clearmember()

        'added  (err)  if there is nothing to delete
        Dim varSelected As String = ""

        Try
          'Dim varSelected As String = lstMembers.SelectedItem.ToString
          varSelected = lstMembers.SelectedItem.ToString
          lstMembers.Items.Remove(varSelected)
        Catch ex As Exception
        End Try

        ' from the array remove that student
        strEnrollments(cboClubs.SelectedIndex, lstGeneral.Items.IndexOf(varselected)) = ""

      End Sub



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

        If Not CheckForInsertErrors() Then Return

        Dim StudIndex As Integer = lstGeneral.SelectedIndex
        Dim name As String = lstGeneral.SelectedItem.ToString()
        Dim clubIndex As Integer = cboClubs.SelectedIndex


        If Not lstMembers.Items.Contains(name) Then
          lstMembers.Items.Add(name)
          intEnrollSizes(clubIndex) += 1
          lblCount.Text = lstMembers.Items.Count.ToString() & " members"

        End If

        'save the names back into the master array
        'For i = 0 To lstMembers.Items.Count - 1
        '  strEnrollments(clubIndex, i) = lstMembers.Items(i).ToString
        'Next

        'replaced
        ' you have the max number of variables in array  - better is to that index of student  write with name
        strEnrollments(clubIndex, StudIndex) = name



      End Sub

     

      Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click

        Clearmember()
        lblCount.Text = lstMembers.Items.Count.ToString() & " members"
      End Sub


      ' added  ( on change of club rearange lstmembers)
      Private Sub cboClubs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClubs.SelectedIndexChanged

        lstMembers.Items.Clear()

        For i = 0 To lstGeneral.Items.Count - 1
          Try
            If strEnrollments(cboClubs.SelectedIndex, i).ToString.Length > 0 Then
              lstMembers.Items.Add(strEnrollments(cboClubs.SelectedIndex, i).ToString)
            End If
          Catch ex As Exception
            'strEnrollments(cboClubs.SelectedIndex, i).ToString.Length   is nothing (error)
          End Try
        Next

      End Sub
    End Class

    Friday, July 20, 2018 10:07 AM
  • Looking at this was very helpful.

    I did find 2 issues that Im working on correcting.

    I found that after you have added someone to a club, switch to another club, and come back to the initial club, it adds the person that you added a second time.

    I also found that when you switch clubs from the drop down the label isnt updated. For example, 2 members in club 1, 6 members in club 2. I add a third member to club 1, the label reads that there are 3 members. I switch to club 2. The label still reads that there are 3 members.

    Monday, July 23, 2018 2:50 AM
  • Hi,  

    1  issue with counter     I added command in cboClubs_SelectedIndexChanged to recalculate them

    Private Sub cboClubs_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboClubs.SelectedIndexChanged

        lstMembers.Items.Clear()

        For i = 0 To lstGeneral.Items.Count - 1
          Try
            If strEnrollments(cboClubs.SelectedIndex, i).ToString.Length > 0 Then
                    lstMembers.Items.Add(strEnrollments(cboClubs.SelectedIndex, i).ToString)
            End If
          Catch ex As Exception
                  'strEnrollments(cboClubs.SelectedIndex, i).ToString.Length   is nothing (error)
          End Try
        Next

         ' this line is added

        lblCount.Text = lstMembers.Items.Count.ToString() & " members"

      End Sub

    what is the second issue - i don't understend

    if You add person to club   it is added to that club  until You leave the program .

    If You wont to clear club members on change of cboClub you can clear all values in array

    strEnrollments  for that club index.

    Monday, July 23, 2018 8:48 AM
  • It will be better if You on listing members in club - clear them from list of students (show only students that aren't members of that club)

    Zeljko

    Croatia

    Monday, July 23, 2018 8:52 AM
  • Hi

    Here is one possibility.

    ' Arrays Dictionary ListBoxes
    
    ' ComboBox1, ListBox1, ListBox2
    ' Button1, Button 2 and Labels
    ' 1 through 5
    
    Option Strict On
    Option Explicit On
    Class Form1
      Dim Dict As New Dictionary(Of String, String())
      Dim Catag() As String = {"Honors", "Golden Arrow", "Computer", "Science"}
      Dim Stud() As String = {"Adams, Ben", "Baker, Sally", "Canseco, Juan", "Davis, Sharon", "Etienne, Jean", "Gonzalez, Jose", "Johnson, Eric", "Koenig, Johann", "Matsunaga, Akiko", "Nakamura, Ken", "Ramirez, Maria", "whitedragon551", "zadzich", "leshay"}
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.Sorted = True
        fillStud()
        For Each s As String In Catag
          Dim temp(0) As String
          temp(0) = Nothing
          Dict.Add(s, temp)
        Next
        ComboBox1.DataSource = Catag
      End Sub
      Sub fillStud()
        For Each s As String In Stud
          If Not ListBox1.Items.Contains(s) Then
            ListBox1.Items.Add(s)
          End If
        Next
        For Each s As String In Stud
          If ListBox2.Items.Contains(s) Then
            ListBox1.Items.Remove(s)
          End If
        Next
        Label5.Text = ListBox2.Items.Count.ToString
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If ListBox1.SelectedItem Is Nothing Then Exit Sub
        Dim ci As Integer = ListBox1.SelectedIndex
        If Not ListBox2.Items.Contains(ListBox1.SelectedItem.ToString) Then
          ListBox2.Items.Add(ListBox1.SelectedItem.ToString)
          Label5.Text = ListBox2.Items.Count.ToString
          If ci < ListBox1.Items.Count - 1 Then
            ListBox1.SelectedIndex += 1
          Else
            ListBox1.SelectedIndex = 0
          End If
          fillStud()
        End If
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If ListBox2.SelectedItem Is Nothing Then Exit Sub
        ListBox2.Items.Remove(ListBox2.SelectedItem.ToString)
        fillStud()
      End Sub
      Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Static cur As String = ComboBox1.SelectedItem.ToString
        Dim a As String()
        ReDim a(ListBox2.Items.Count - 1)
        ListBox2.Items.CopyTo(a, 0)
        Dict(cur) = a
        cur = ComboBox1.SelectedItem.ToString
        ListBox2.Items.Clear()
        For Each s As String In Dict(cur)
          If Not s Is Nothing Then
            ListBox2.Items.Add(s)
          End If
        Next
        fillStud()
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Monday, July 23, 2018 12:26 PM