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!
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
'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)
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
'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.
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.
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
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