Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

From Inputbox to Listbox

accordpcaccordpc Posts: 1Member
Alright folks...I'm new to this visual basic stuff, and to the site. I am working on an assignment for a class...it'll probably look pretty easy to the rest of you, but I just can't figure it out.

What I need to do is take the information from the input box, the 'quiz scores' that are entered and put them in the list box. The way it is set up now, as you'll see is that it takes the last score and enters it all 5 times. It appears that all of my other calculations work. I'm not looking for someone to completely write the code for me, as I really need to and want to learn this...but maybe just a little 'shove' in the right direction?!

Below is all of the code I have so far: I changed the part I need help with to red font so you can find it quicker...Thanks!!!!!

' Project: IT Department Grades
' Purpose: Allows the user to enter 5 scores, they will be displayed in a list box
' and then can be used to calculate the average and a grade letter will
' be displayed.
' Created by: Lisa Bastian on 9/27/08

Option Strict On
Option Explicit On


Public Class GradeForm

Dim strInputQuiz As String
Dim intQuizCounter As Integer = 1
Dim strGrade As String
Dim intQuizPoints As Integer
Dim intPointsAccumulator As Integer
Dim intAverage As Integer
Dim isConverted As Boolean

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
lblTotalPoints.Text = ""
lblAveragePoints.Text = ""
lblLetterGrade.Text = ""
lstGrades.Items.Clear()


End Sub

Private Sub btnAcceptGrades_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAcceptGrades.Click


' repeats input box until all 5 grades have been entered
Do While intQuizCounter < 6
' input box for user to enter the numeric grades
strInputQuiz = InputBox("Please enter quiz grades " & intQuizCounter, "Quiz Grades")

' if points entered are correct numbers count the quiz and add the points
' in the points accumulator if not show message box saying re-enter
isConverted = Integer.TryParse(strInputQuiz, intQuizPoints)
If isConverted Then
intQuizCounter = intQuizCounter + 1
intPointsAccumulator = intPointsAccumulator + intQuizPoints

Else
MessageBox.Show("Please re-enter quiz grade.", "Quiz Grades Error", MessageBoxButtons.OK, _
MessageBoxIcon.Information)

End If
Loop
[color=Red] ' Converts the Quiz Points entered to string and adds them
' to the list box
lstGrades.Text = Convert.ToString(intQuizPoints)
lstGrades.Items.Add(intQuizPoints)
lstGrades.Items.Add(intQuizPoints)
lstGrades.Items.Add(intQuizPoints)
lstGrades.Items.Add(intQuizPoints)
lstGrades.Items.Add(intQuizPoints)[/color]

End Sub

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
' uses the numeric grades entered in the list box
' to give the average grade, which will be displayed as a
' letter grade

lblTotalPoints.Text = Convert.ToString(intPointsAccumulator)

lblAveragePoints.Text = Convert.ToString(intPointsAccumulator / 5)

intAverage = CInt(Convert.ToString(intPointsAccumulator / 5))
lblAveragePoints.Text = Convert.ToString(intAverage)

Select Case intAverage
Case Is >= 93
strGrade = "A"
Case Is >= 86
strGrade = "B"
Case Is >= 77
strGrade = "C"
Case Is >= 70
strGrade = "D"
Case Else
strGrade = "F"
End Select

' displays the letter grade in the label
lblLetterGrade.Text = strGrade
End Sub
End Class

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    If I understand correctly, you want to prompt the user to enter a value and then add that to your listbox? I have found that "InputBoxes" are annoying to the user, but if this is a tool for yourself or your IT Dept, then it shouldn't be too bad. If you will have a lot of end users, I would suggest having a button skim values from textboxes.

    To do what you want:

    [code]
    Dim Val as String = ""
    Dim dPercent as Double = 0

    Val = InputBox("Please enter a Quiz Score.", "Score")
    If isNumeric(Val) then
    dPercent = cDbl(Val)
    'any other code you'd like, we just filled dPercent with the entered quiz score
    MsgBox("You entered a numeric value of " & dPercent.toString())
    Else
    MsgBox("You must enter a numeric value.)
    End If

    [/code]

    So let's say for instance you would like to continue to prompt the user until he enters a good value. This would be how you do that.

    [code]
    Dim Val as String = ""
    Dim dPercent as Double = 0
    Dim Resolve as Boolean = false

    Do While Not Resolve

    Val = InputBox("Please enter a Quiz Score.", "Score")

    If isNumeric(Val) then
    dPercent = cDbl(Val)
    Resolve = True
    Else
    MsgBox("Please enter a numeric value.")
    End If

    Loop

    [/code]

    Happy Coding,
    FireSickle.com
Sign In or Register to comment.