Displaying Grades in a ListBox Based on numbers from another listbox

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 490 Views - Last Post: 24 June 2012 - 03:59 AM Rate Topic: -----

#1 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 02:35 AM

I enter Names and scores from a text box. Once i press "show me the array" my listbox gets populated with the names and scores.
A "grade" button is then to be pressed to determine the grades of the scores.
e.g.
James | 98 | High Distinction
BOb | 23 | Participation

note: High Distinction 90 and above;
Distinction 75 up to 89;
Credit 60 up to 74; and
Pass 50 up to 59,
all other students will be awarded a Participation Grade

All my code is as followed:

Public Class frmCreateArray
    Dim Names(21) As String
    Dim NamesCount, ListCount As Integer
    Dim Ages(21) As Double
    Dim AgesCount, ListCount2 As Integer
    Dim Grades(21) As String
    Dim GradesCount, ListCount3 As Integer
    Dim Total As Double
    Dim Item As Double

    Private Property items As Object

    Private Property MsgBox As String

    Private Sub cmdEnterThisName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnterThisName.Click
        For Ages(AgesCount) = 1 To AgesCount
            If Ages(AgesCount) >= 90 Then
                Grades(GradesCount) = "High Destinction"
            ElseIf Ages(AgesCount) >= 75 Then
                Grades(GradesCount) = "Destinction"
            ElseIf Ages(AgesCount) >= 60 Then
                Grades(GradesCount) = "Credit"
            ElseIf Ages(AgesCount) >= 50 Then
                Grades(GradesCount) = "Pass"
            ElseIf Ages(AgesCount) < 50 Then
                Grades(GradesCount) = "Participation"
            End If
        Next
        NamesCount = NamesCount + 1
        Names(NamesCount) = txtNamesGoHere.Text
        AgesCount = AgesCount + 1
        Ages(AgesCount) = txtAgesGoHere.Text
        GradesCount = GradesCount + 1
        lblCounter.Text = Str(NamesCount) + " Students Entered"
        lblCounter.Text = Str(AgesCount) + " Students Entered"
        txtNamesGoHere.Text = "" : txtAgesGoHere.Text = ""
        txtNamesGoHere.Focus()
    End Sub
    Private Sub frmCreateArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NamesCount = 0
        AgesCount = 0
        GradesCount = 0
    End Sub

    Private Sub cmdShowListArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShowListArray.Click
        For Me.ListCount = 1 To NamesCount
            lstListNames.Items.Add(Names(ListCount))
        Next
        For Me.ListCount2 = 1 To AgesCount
            lstListAges.Items.Add(Ages(ListCount2))
        Next
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        txtNamesGoHere.Text = ""
        txtAgesGoHere.Text = ""
        lblavg.Text = "..."
        lstListNames.Items.Clear()
        lstListAges.Items.Clear()
        lstGrade.Items.Clear()
        lblCounter.Text = " Number of names entered"
        txtNamesGoHere.Focus()
        NamesCount = 0
        AgesCount = 0
        lblavgexplain.Text = "..."
    End Sub

    Private Sub btnAvg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAvg.Click
        For Each Me.Item In lstListAges.Items
            Total = 0
            For Me.AgesCount = 0 To AgesCount
                Total = Total + Ages(AgesCount)
            Next
            lblavg.Text = Total / NamesCount
            lblavgexplain.Text = "The average number of scores is: "
        Next
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
        txtNamesGoHere.Text = ""
        txtAgesGoHere.Text = ""
        txtNamesGoHere.Focus()
    End Sub

    Private Sub btnGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrade.Click
        For Me.GradesCount = 1 To AgesCount
            lstGrade.Items.Add(Grades(GradesCount))
        Next
    End Sub
End Class


i get troubles when i press "grade" i need a way to tell VB to populate the listbox


I am very new to VB, please help :(
I have been stressing over this for weeks :'( :stupid:

Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Grades in a ListBox Based on numbers from another listbox

#2 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 12:49 PM

Quote

i get troubles when i press "grade" i need a way to tell VB to populate the listbox
what kind of troubles?
Was This Post Helpful? 0
  • +
  • -

#3 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 04:16 PM

View Postsela007, on 23 June 2012 - 12:49 PM, said:

Quote

i get troubles when i press "grade" i need a way to tell VB to populate the listbox
what kind of troubles?


This is my error.

http://puu.sh/Dbdt

:(
Was This Post Helpful? 0
  • +
  • -

#4 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 04:31 PM

You have declare Grades(21) but you haven't assign values.
For example:
If you initialize Grades(21) and try add Grades(1) to listbox the same error will be raised.
Public Grades(21) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ListBox1.Items.Add(Grades(1))
End Sub

But if you assign value before you add it to the listbox the error will be fixed.
Public Grades(21) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Grades(1)="Something"
 ListBox1.Items.Add(Grades(1))
End Sub
i hope now you know where is the problem

This post has been edited by sela007: 23 June 2012 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#5 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 04:39 PM

View Postsela007, on 23 June 2012 - 04:31 PM, said:

You have initialized Grades(21) but you haven't assign values.
For example:
If you initialize Grades(21) and try add Grades(1) to listbox the same error will be raised.
Public Grades(21) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 ListBox1.Items.Add(Grades(1))
End Sub

But if you assign value before you add it to the listbox the error will be fixed.
Public Grades(21) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Grades(1)="Something"
 ListBox1.Items.Add(Grades(1))
End Sub


The values of lstGrades comes from the scores of the score array (which is named 'Ages') So the amount of scores and what they are will always be changing i cant preset the array. It all depends on what the user inputs. :/
Was This Post Helpful? 0
  • +
  • -

#6 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 04:47 PM

Just assign values to Grades before program starts.
Put this code in form load event. And try again.
For i = 0 to 21
Grades(i)=""
Next

Was This Post Helpful? 0
  • +
  • -

#7 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 04:55 PM

I debugged it, and whenever i enter an number it displays "high distinction" and then when i input another number and name it puts it twice.

Private Sub frmCreateArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        NamesCount = 0
        AgesCount = 0
        For i = 0 To 21
            Grades(i) = ""
        Next
    End Sub



What about the part where it displays the grades:
    Private Sub btnGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrade.Click
        For Me.GradesCount = 1 To AgesCount
            lstGrade.Items.Add(Grades(GradesCount))
        Next
    End Sub
End Class



Or where it actually gets the grades:
Private Sub cmdEnterThisName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEnterThisName.Click
        For Ages(AgesCount) = 1 To AgesCount
            If Val(txtAgesGoHere.Text) >= 90 < 100 Then
                lstGrade.Items.Add("High Destinction")
            ElseIf Val(txtAgesGoHere.Text) >= 75 < 89 Then
                lstGrade.Items.Add("Destinction")
            ElseIf Val(txtAgesGoHere.Text) >= 60 < 74 Then
                lstGrade.Items.Add("Credit")
            ElseIf Val(txtAgesGoHere.Text) >= 50 < 59 Then
                lstGrade.Items.Add("Pass")
            ElseIf Val(txtAgesGoHere.Text) < 50 Then
                lstGrade.Items.Add("Participation")
            End If
        Next
        NamesCount = NamesCount + 1
        Names(NamesCount) = txtNamesGoHere.Text
        AgesCount = AgesCount + 1
        Ages(AgesCount) = txtAgesGoHere.Text
        GradesCount = GradesCount + 1
        lblCounter.Text = Str(NamesCount) + " Students Entered"
        lblCounter.Text = Str(AgesCount) + " Students Entered"
        txtNamesGoHere.Text = "" : txtAgesGoHere.Text = ""
        txtNamesGoHere.Focus()
    End Sub


(thank you for the help)
Was This Post Helpful? 0
  • +
  • -

#8 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 05:13 PM

Quote

when i input another number and name it puts it twice.

before you add grades to the listbox clear the listbox.

Quote

whenever i enter an number it displays "high distinction"

something is wrong in with else/if statement in part where you gets the grades.
Was This Post Helpful? 0
  • +
  • -

#9 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 05:19 PM

I recommend you to use select case statemet.
Select Case Val(txtAgesGoHere.Text)
            Case Is > 100
                lstGrade.Items.Add("High Destinction")
            Case Is > 75
                lstGrade.Items.Add("Destinction")
'...
        End Select

Was This Post Helpful? 1
  • +
  • -

#10 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 08:17 PM

View Postsela007, on 23 June 2012 - 05:19 PM, said:

I recommend you to use select case statemet.
Select Case Val(txtAgesGoHere.Text)
            Case Is > 100
                lstGrade.Items.Add("High Destinction")
            Case Is > 75
                lstGrade.Items.Add("Destinction")
'...
        End Select


THAT WORKS! Very Well!
If i enter the person it immediately shows the grade
BUT i need them to show when i press the 'Grade" button.
when i press 'grade' it just says Participation when i enter 99.
I need some code to display the listbox correctly for when i press "Grade"
Sooo close :)
Was This Post Helpful? 0
  • +
  • -

#11 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 23 June 2012 - 10:14 PM

Quote

If i enter the person it immediately shows the grade
BUT i need them to show when i press the 'Grade" button.

then move the part of code which shows the grade, to the grade button click event. i don't know where is the problem.

Quote

when i press 'grade' it just says Participation when i enter 99.
please show the code from 'grade' click.
Was This Post Helpful? 0
  • +
  • -

#12 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 24 June 2012 - 01:20 AM

View Postsela007, on 23 June 2012 - 10:14 PM, said:

Quote

when i press 'grade' it just says Participation when i enter 99.
please show the code from 'grade' click.


Private Sub btnGrade_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrade.Click
        For Me.ListCount2 = 1 To AgesCount
            lstGrade.Items.Add(Grades(ListCount2))
        Next
    End Sub


That's it. I think once this is solved it should work :)
Was This Post Helpful? 0
  • +
  • -

#13 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 24 June 2012 - 03:26 AM

If i enter the person it immediately shows the grade
BUT i need them to show when i press the 'Grade" button.

you must try to figure this by yourself, all you need is to remove the code which shows the grade from the "enter the person" procedure.
Was This Post Helpful? 0
  • +
  • -

#14 Rmclayton  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 46
  • Joined: 20-June 12

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 24 June 2012 - 03:31 AM

I've fixed it. the grade does not show when i enter the number
But when i press "grade" it only comes up with one grade.

e.g.
I entered '12'
then i entered '99'
and it said "High distinction"

I think its only getting the grade from the LAST score entered :/
Was This Post Helpful? 0
  • +
  • -

#15 sela007  Icon User is offline

  • D.I.C Addict

Reputation: 137
  • View blog
  • Posts: 832
  • Joined: 21-December 11

Re: Displaying Grades in a ListBox Based on numbers from another listbox

Posted 24 June 2012 - 03:32 AM

Quote

If i enter the person it immediately shows the grade
BUT i need them to show when i press the 'Grade" button.

you must try to figure this by yourself. All you need is to remove the part of code which "shows" the grades ,from the "enter the person" procedure.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2