VB.NET School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 300,513 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,947 people online right now. Registration is fast and FREE... Join Now!




Grade program with list boxes

 

Grade program with list boxes, Shows average plus lettter grade

southerngenes

4 Jul, 2009 - 05:38 AM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2009
Posts: 27

I'm trying to code a program that averages a grade from a list box, but the numbers in the second list box could change, so not sure how to code that. It should list the letter grade and the average, then if they don't enter a grade, should tell you to enter a grade first before hitting the average button.
CODE

Public Class MainForm

        
    Dim ErrorMsg As String
    Dim intNum As Object
    Dim intremove As Object
    Dim AllGrades As Integer  
    Dim NumberofGrades As Integer 'counter for the number of items
    Dim average As Double         'average value of the grades
    Dim gradeLtr As String        'letter grade

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'fill the list box with grades 50-100
        For grade As Integer = 50 To 100 Step 5
            Me.lstbox1.Items.Add(grade.ToString("N0"))
        Next grade

        Me.lstbox1.SelectedIndex = 0 'set the 0 as the default one

    End Sub

    Private Sub calcAvgBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcAvgBtn.Click
        
'local variables
        Dim ex As Exception
        ex = Nothing
        ErrorMsg = ""

        Try

        Catch ex
            ErrorMsg = "Error clicking the Submit: " & ex.Message & "!"

        Finally
            If ErrorMsg.Length > 0 Then
                MessageBox.Show(ErrorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End Try

        If ErrorMsg.Length = 0 Then
            'calculate the average
            average = AllGrades / NumberofGrades ‘ this isn’t working!

            'get letter grade of grades – not sure how to do this
            ' gradeLtr =

            'compute the average
            Select Case average
                Case 90 To 100
                    gradeLtr = "A"
                Case 80 To 89
                    gradeLtr = "B"
                Case 70 To 79
                    gradeLtr = "C"
                Case 60 To 69
                    gradeLtr = "D"
                Case Else
                    gradeLtr = "F"

            End Select
        Else
            Msg("You need to add grades to be averaged.", "Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        Msg("Average: " & average)
        Msg("Grade: " & gradeLtr)

        'error message if no numbers are chosen goes here???


        'clean up variables
        ex = Nothing
        ErrorMsg = Nothing
        average = Nothing
        gradeLtr = Nothing
        AllGrades = Nothing
        NumberofGrades = Nothing

    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        'gradeLtr.text = ""
        'intcounter = 0 ' reset the counter for items to avoid error – didn’t work
        lstbox2.Items.Clear()
    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click

        'adding selected item in the lstbox1 to lstbox2 >>
        intNum = lstbox1.SelectedItem
        lstbox2.Items.Add(intNum)

    End Sub

    Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click

        'removing seleced item in lstbox2 <<
        intremove = lstbox2.SelectedItem
        lstbox2.Items.Remove(intremove)

    End Sub
End Class



User is offlineProfile CardPM
+Quote Post


southerngenes

RE: Grade Program With List Boxes

4 Jul, 2009 - 11:36 AM
Post #2

New D.I.C Head
*

Joined: 29 Jan, 2009
Posts: 27

QUOTE(southerngenes @ 4 Jul, 2009 - 05:38 AM) *

I'm trying to code a program that averages a grade from a list box, but the numbers in the second list box could change, so not sure how to code that. It should list the letter grade and the average, then if they don't enter a grade, should tell you to enter a grade first before hitting the average button.
CODE

Public Class MainForm

        
    Dim ErrorMsg As String
    Dim intNum As Object
    Dim intremove As Object
    Dim AllGrades As Integer  
    Dim NumberofGrades As Integer 'counter for the number of items
    Dim average As Double         'average value of the grades
    Dim gradeLtr As String        'letter grade

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'fill the list box with grades 50-100
        For grade As Integer = 50 To 100 Step 5
            Me.lstbox1.Items.Add(grade.ToString("N0"))
        Next grade

        Me.lstbox1.SelectedIndex = 0 'set the 0 as the default one

    End Sub

    Private Sub calcAvgBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcAvgBtn.Click
        
'local variables
        Dim ex As Exception
        ex = Nothing
        ErrorMsg = ""

        Try

        Catch ex
            ErrorMsg = "Error clicking the Submit: " & ex.Message & "!"

        Finally
            If ErrorMsg.Length > 0 Then
                MessageBox.Show(ErrorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            End If
        End Try

        If ErrorMsg.Length = 0 Then
            'calculate the average
            average = AllGrades / NumberofGrades ‘ this isn’t working!

            'get letter grade of grades – not sure how to do this
            ' gradeLtr =

            'compute the average
            Select Case average
                Case 90 To 100
                    gradeLtr = "A"
                Case 80 To 89
                    gradeLtr = "B"
                Case 70 To 79
                    gradeLtr = "C"
                Case 60 To 69
                    gradeLtr = "D"
                Case Else
                    gradeLtr = "F"

            End Select
        Else
            Msg("You need to add grades to be averaged.", "Error", _
                MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        Msg("Average: " & average)
        Msg("Grade: " & gradeLtr)

        'error message if no numbers are chosen goes here???


        'clean up variables
        ex = Nothing
        ErrorMsg = Nothing
        average = Nothing
        gradeLtr = Nothing
        AllGrades = Nothing
        NumberofGrades = Nothing

    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        'gradeLtr.text = ""
        'intcounter = 0 ' reset the counter for items to avoid error – didn’t work
        lstbox2.Items.Clear()
    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click

        'adding selected item in the lstbox1 to lstbox2 >>
        intNum = lstbox1.SelectedItem
        lstbox2.Items.Add(intNum)

    End Sub

    Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click

        'removing seleced item in lstbox2 <<
        intremove = lstbox2.SelectedItem
        lstbox2.Items.Remove(intremove)

    End Sub
End Class



The only question I have is how to get it to average the 2nd list box, since there can be different amounts of numbers in that box.
User is offlineProfile CardPM
+Quote Post

kewlkreator

RE: Grade Program With List Boxes

4 Jul, 2009 - 11:40 AM
Post #3

Total DIC-Wad
Group Icon

Joined: 25 Mar, 2009
Posts: 1,023



Thanked: 16 times
Dream Kudos: 550
My Contributions
I made a grade calculator... posted here:
http://www.mediafire.com/?sharekey=4cb95ed...621d66e282a0ee8
User is offlineProfile CardPM
+Quote Post

southerngenes

RE: Grade Program With List Boxes

4 Jul, 2009 - 12:38 PM
Post #4

New D.I.C Head
*

Joined: 29 Jan, 2009
Posts: 27

QUOTE(kewlkreator @ 4 Jul, 2009 - 11:40 AM) *

I made a grade calculator... posted here:
http://www.mediafire.com/?sharekey=4cb95ed...621d66e282a0ee8


I couldn't look at what you have. I tried downloading it, but it doesn't work...Just need to figure out how to get the 2nd list box to add up correctly and do an average since there could be one number in that box, or several. I know I need some type of accumulator, just not sure how to finish it.
User is offlineProfile CardPM
+Quote Post

LoveIsNull

RE: Grade Program With List Boxes

4 Jul, 2009 - 06:05 PM
Post #5

Disbanding my Ignorance
****

Joined: 10 Mar, 2009
Posts: 536



Thanked: 45 times
My Contributions
You can try this out, I tried to add in some comments for guidance.

CODE
  'Dim ErrorMsg As String
    Dim intNum As Integer 'Should probably not be of type Object if you specifically want an integer
    Dim intremove As Integer 'Object
    Dim AllGrades As Integer
    Dim NumberofGrades As Integer 'counter for the number of items
    Dim average As Double         'average value of the grades
    Dim gradeLtr As String        'letter grade

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'fill the list box with grades 50-100
        For grade As Integer = 50 To 100 Step 5
            Me.lstbox1.Items.Add(grade.ToString("N0"))
        Next grade

        Me.lstbox1.SelectedIndex = 0 'set the 0 as the default one

    End Sub

    Private Sub calcAvgBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcAvgBtn.Click

        'local variables: Note, these are not really needed here.
        'Dim ex As Exception
        'ex = Nothing
        'ErrorMsg = ""
        '=====================================================================
        'This try block is useless here anyways, you'd need to put the code between
        'try and catch for it to catch an excpetion. Code after "catch" runs if an exception is thrown
        'Code after "finally" always runs.
        'Try

        'Catch ex
        '    ErrorMsg = "Error clicking the Submit: " & ex.Message & "!"

        'Finally
        '    If ErrorMsg.Length > 0 Then
        '        MessageBox.Show(ErrorMsg, "Error", MessageBoxButtons.OK, _
        'MessageBoxIcon.Exclamation)
        '    End If
        'End Try
        '================================================================

        'If ErrorMsg.Length = 0 Then
        'This make sure there is something in the list, if this is the list
        'whose contents you want to add.
        If lstbox2.Items.Count > 0 Then
            'If this list contains something then
            'You need to add up all the grades first, reset these counters first
            AllGrades = 0
            NumberofGrades = 0
            'iterate through the list,
            'NOTE: If 'item' turns out to not be a number, an exception will likely be thrown
            For Each item As Integer In lstbox2.Items
                AllGrades += item 'Add to total grade count
                NumberofGrades += 1 'Add to number of grades
            Next
            'calculate the average
            average = AllGrades / NumberofGrades ' this isn’t working!
            'Was not working because neither had a value, be sure you add up the grades
            'and count the number of them first.

            'get letter grade of grades – not sure how to do this
            ' gradeLtr =

            'compute the average
            'This should be sufficient for now
            Select Case average
                Case Is > 89        '90 and above is considered an A
                    gradeLtr = "A"
                Case Is > 79
                    gradeLtr = "B"
                Case Is > 69
                    gradeLtr = "C"
                Case Is > 59
                    gradeLtr = "D"
                Case Is < 60        'Less than 60 considered F
                    gradeLtr = "F"

            End Select

            'Display messageboxes here.
            MessageBox.Show("Average: " & average)
            MessageBox.Show("Grade: " & gradeLtr)
        Else
            'If there is nothing in lstbox2 we will show this message instead
            MessageBox.Show("You need to add grades to be averaged.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If



        'NOTE: I just don't think the below lines are necessary
        'clean up variables
        'ex = Nothing
        'ErrorMsg = Nothing
        'average = Nothing
        'gradeLtr = Nothing
        'AllGrades = Nothing
        'NumberofGrades = Nothing

    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        'gradeLtr.text = ""
        'intcounter = 0 ' reset the counter for items to avoid error – didn’t work
        lstbox2.Items.Clear()
    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click

        'adding selected item in the lstbox1 to lstbox2 >>
        intNum = lstbox1.SelectedItem
        lstbox2.Items.Add(intNum)

    End Sub

    Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click

        'removing seleced item in lstbox2 <<
        intremove = lstbox2.SelectedItem
        lstbox2.Items.Remove(intremove)
    End Sub

User is offlineProfile CardPM
+Quote Post

southerngenes

RE: Grade Program With List Boxes

4 Jul, 2009 - 07:11 PM
Post #6

New D.I.C Head
*

Joined: 29 Jan, 2009
Posts: 27

QUOTE(LoveIsNull @ 4 Jul, 2009 - 06:05 PM) *

You can try this out, I tried to add in some comments for guidance.

CODE
  'Dim ErrorMsg As String
    Dim intNum As Integer 'Should probably not be of type Object if you specifically want an integer
    Dim intremove As Integer 'Object
    Dim AllGrades As Integer
    Dim NumberofGrades As Integer 'counter for the number of items
    Dim average As Double         'average value of the grades
    Dim gradeLtr As String        'letter grade

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'fill the list box with grades 50-100
        For grade As Integer = 50 To 100 Step 5
            Me.lstbox1.Items.Add(grade.ToString("N0"))
        Next grade

        Me.lstbox1.SelectedIndex = 0 'set the 0 as the default one

    End Sub

    Private Sub calcAvgBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcAvgBtn.Click

        'local variables: Note, these are not really needed here.
        'Dim ex As Exception
        'ex = Nothing
        'ErrorMsg = ""
        '=====================================================================
        'This try block is useless here anyways, you'd need to put the code between
        'try and catch for it to catch an excpetion. Code after "catch" runs if an exception is thrown
        'Code after "finally" always runs.
        'Try

        'Catch ex
        '    ErrorMsg = "Error clicking the Submit: " & ex.Message & "!"

        'Finally
        '    If ErrorMsg.Length > 0 Then
        '        MessageBox.Show(ErrorMsg, "Error", MessageBoxButtons.OK, _
        'MessageBoxIcon.Exclamation)
        '    End If
        'End Try
        '================================================================

        'If ErrorMsg.Length = 0 Then
        'This make sure there is something in the list, if this is the list
        'whose contents you want to add.
        If lstbox2.Items.Count > 0 Then
            'If this list contains something then
            'You need to add up all the grades first, reset these counters first
            AllGrades = 0
            NumberofGrades = 0
            'iterate through the list,
            'NOTE: If 'item' turns out to not be a number, an exception will likely be thrown
            For Each item As Integer In lstbox2.Items
                AllGrades += item 'Add to total grade count
                NumberofGrades += 1 'Add to number of grades
            Next
            'calculate the average
            average = AllGrades / NumberofGrades ' this isn’t working!
            'Was not working because neither had a value, be sure you add up the grades
            'and count the number of them first.

            'get letter grade of grades – not sure how to do this
            ' gradeLtr =

            'compute the average
            'This should be sufficient for now
            Select Case average
                Case Is > 89        '90 and above is considered an A
                    gradeLtr = "A"
                Case Is > 79
                    gradeLtr = "B"
                Case Is > 69
                    gradeLtr = "C"
                Case Is > 59
                    gradeLtr = "D"
                Case Is < 60        'Less than 60 considered F
                    gradeLtr = "F"

            End Select

            'Display messageboxes here.
            MessageBox.Show("Average: " & average)
            MessageBox.Show("Grade: " & gradeLtr)
        Else
            'If there is nothing in lstbox2 we will show this message instead
            MessageBox.Show("You need to add grades to be averaged.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If



        'NOTE: I just don't think the below lines are necessary
        'clean up variables
        'ex = Nothing
        'ErrorMsg = Nothing
        'average = Nothing
        'gradeLtr = Nothing
        'AllGrades = Nothing
        'NumberofGrades = Nothing

    End Sub

    Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
        'gradeLtr.text = ""
        'intcounter = 0 ' reset the counter for items to avoid error – didn’t work
        lstbox2.Items.Clear()
    End Sub

    Private Sub addBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addBtn.Click

        'adding selected item in the lstbox1 to lstbox2 >>
        intNum = lstbox1.SelectedItem
        lstbox2.Items.Add(intNum)

    End Sub

    Private Sub removeBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeBtn.Click

        'removing seleced item in lstbox2 <<
        intremove = lstbox2.SelectedItem
        lstbox2.Items.Remove(intremove)
    End Sub


Will try it. THANKS!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 05:18AM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month