need help with listbox items

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jac130
    New Member
    • Oct 2008
    • 18

    need help with listbox items

    I am trying to calculate the class average of student's grades. the student names are displayed in a list box, while their corresponding grade is displayed in a separate list box. calculate the average: # of students/ sum of their grades.(grades are numeric)

    EX: John 90
    sally 88
    mike 75

    I can't get it to add the sum of the grades i.e. (90+88+75). Also, I won't know how many students and grades the user will enter into the list. so it has to be able to add any number of grades.
    any help would be great. thanks.
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    You can Loop through and find the Average:

    [code=vb]
    Dim i As Integer
    Dim TMarks As Long
    TMarks =0
    txtAvg.Text = "0.00"
    For i = 0 To lstMarks.ListCo unt-1
    TMarks = TMarks + Val(lstMarks.Li st(i))
    Next
    If lstMarks.ListCo unt >0 Then
    txtAvg.Text = Format((TMarks/lstMarks.ListCo unt),"0.00")
    End If
    [/code]

    Regards
    Veena

    Comment

    • jac130
      New Member
      • Oct 2008
      • 18

      #3
      Thanks a lot, that helped. I also am having trouble with one other aspect. once the names and grades are displayed (in separate list boxes), there is a button, prompting the user to a certain grade, I must display a message box listing all of the student names below that certain grade. I've looped it to ask the user/ validate input, but it crashes before displaying the message box with all the names. Any thoughts? Here is a portion of my code:

      Function GetBelowGrade() As Double
      Dim onegrade As String = InputBox("pleas e enter a positive number for the grade.")
      Do While Not IsPositiveNumbe r(onegrade)
      onegrade = InputBox("pleas e enter a positive number for the grade.")
      Loop
      Dim i As Integer
      For i = 0 To lstGrades.Items .Count - 1
      Next

      If lstGrades.Items (i) < onegrade Then
      Return (MsgBox(lstStud ents.Items(lstG rades.Items(i)) ))
      End If
      End Function

      Private Sub btnBelowGrade_C lick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnBelowGrade.C lick
      GetBelowGrade()

      End Sub
      End Class

      any help would be great

      Comment

      • debasisdas
        Recognized Expert Expert
        • Dec 2006
        • 8119

        #4
        why using inputbox twice ?

        your variable is a string ,which you are using to compare using < perator without any conversion.

        Comment

        • jac130
          New Member
          • Oct 2008
          • 18

          #5
          I changed it to this. but it says "onegrade" and "i" are used before they are assigned a value and could result in a null reference exception. Does that not matter?
          i changed it to a string and it still crashes.
          Function GetBelowGrade() As Double
          Dim onegrade As String
          Do While Not IsPositiveNumbe r(onegrade)
          onegrade = InputBox("pleas e enter a positive number for the grade.")
          Loop
          Dim i As String = lstGrades.Items (i)
          If lstGrades.Items (i) < onegrade Then
          Return MsgBox(lstGrade s.Items(lstStud ents.Items(i)))
          End If

          If lstGrades.Items (i) < onegrade Then
          Return (MsgBox(lstStud ents.Items(lstG rades.Items(i)) ))
          End If
          End Function

          Comment

          • jac130
            New Member
            • Oct 2008
            • 18

            #6
            there would be two list boxes with names and grades: ex sally 90
            mike 85

            i click the getbelowgrade button, and am prompted to enter a grade, say 88. it should display Mike's name (he's below 88), but it crashes and highlights the Return line of the function, saying value 88 is not valid for index.
            I'm stuck, here is my the portion of my code. Thanks.

            Function GetBelowGrade() As String
            Dim aGrade As String = InputBox("pleas e enter a positive number for the grade.")
            Do While Not IsPositiveNumbe r(aGrade)
            aGrade = InputBox("pleas e enter a positive number for the grade.")
            Loop
            Dim i As Integer
            For i = 0 To lstGrades.Items .Count - 1 Step lstGrades.Items (i) < aGrade
            Return MsgBox(lstStude nts.Items(lstGr ades.Items(i)))
            Next
            End Function


            Private Sub btnBelowGrade_C lick(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnBelowGrade.C lick
            GetBelowGrade()

            End Sub
            End Class

            Comment

            • QVeen72
              Recognized Expert Top Contributor
              • Oct 2006
              • 1445

              #7
              Hi,

              You may have One or more Students below the Average Grade, you need to Build a String and then Return List of the names..
              Also, you are not returning anything, You can do with a Sub-Procedure
              Check This Code:

              [code=vb]
              Sub GetBelowGrade() As String
              Dim aGrade As String = InputBox("pleas e enter a positive number for the grade.")
              Do While Not IsPositiveNumbe r(aGrade)
              aGrade = InputBox("pleas e enter a positive number for the grade.")
              Loop
              Dim NGrade As Integer
              Dim strList As String =""
              NGrade = Val(aGrade)
              Dim i As Integer
              '
              For i = 0 To lstGrades.Items .Count - 1
              If Val(lstGrades.I tems(i)) < NGrade Then
              strList = strList & lstStudents.Ite ms(i) & ", "
              End If
              Next
              MsgBox strList
              End Sub
              [/code]

              Regards
              Veena

              Comment

              • freefony
                New Member
                • Nov 2008
                • 59

                #8
                Am so sorry am using this medium to get answers am working on a similar projects but unlike him am displaying my datas in a table and i need to calculate the value for just one of the column units to calculate the total units of all the courses registered by a student also my connection to the database cannot be accessed cos its said to be in a private dim even tho i decleared it in a public sub . am new to programming and i need all possible assistance

                Comment

                • jac130
                  New Member
                  • Oct 2008
                  • 18

                  #9
                  thanks. I did try that code and it helped greatly. Although, I did create it as a function, i was required to. i just returned a message box with the string list.
                  If i wanted to eliminate just the listbox of the grades (not the student names) from the interface, and store the grades into an array, then use the array in the functions,
                  I'm just lookin for adivce on how to start.for example.. in my current code i have:

                  dim grade as string=inputbox ("please enter grades")
                  lstgrades.items .add(grades)

                  I have to delete the list box, how do i get those grades into an array? do i create a class level array: dim grades() as integer ? should i not specifiy a max index? or something like: dim grades() as integer = val(grade) ?
                  or create an array for each function. ?

                  any thoughts/adivce would be great

                  Comment

                  Working...