English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

On my Program a user enters a subject score in a text boxeg txtMaths.

I want code to ensure that only a score between 0 and 100 inclusive is accepted.

Also I want code to assign a grade for the score entered in the text box above eg
100-80 A
79-60 B
59-40 C
39-0 D
The score is entered in the text box and the grade id displayed in a label eg lblMathsgr

If I have a list of subjects and a list of corresponding text boxes(for scores) and labels ( for the grades) should I use a control array? will I have to repeat the code if each box does the same thing ie accept input and display grade.

2007-03-13 15:13:34 · 3 answers · asked by candy 1 in Computers & Internet Programming & Design

3 answers

Here's the simplest way to do it in vb6:

Private Sub txtMaths_Change()
On Error Resume Next
If CInt(txtMaths) < 0 Then txtMaths = 0
If CInt(txtMaths) > 100 Then txtMaths = 100

Select Case CInt(txtMaths)
Case 0 To 39: lblMathsgr = "D"
Case 40 To 59: lblMathsgr = "C"
Case 60 To 79: lblMathsgr = "B"
Case 80 To 100: lblMathsgr = "A"
End Select
End Sub

- The 'On Error Resume Next' takes care of the error when a value that cannot be converted to an Integer is entered. You can add a Error message to a label or something if you wish (a msgbox would be too annoying, as it could potentially keep popping up). The rest is pretty self explanatory, you just make sure the number is 0 to 100, and use a select case on the integer range to set the letter grade.

Edit - For adding several subjects, you're right I would just use a control array. I would put txtGrade and and lblLetter in a frame named fraSubject. Then copy+paste and whole frame, allowing it to make an array, for each subject. The code would be exactly the same for 'txtGrade_Change(Index As Integer)', just replace each 'txtMaths' with 'txtGrade(Index)' and each 'lbllblMathsgr' with 'lblLetter(Index)'.

2007-03-13 15:21:20 · answer #1 · answered by Tim 6 · 0 0

i'm not sure which Visual Basic you are using, but in Vb.net you could do this


dim intScore as integer
intScore = Val(txtMaths.text)

Select Case intScore
Case 100 to 80
lblMaths.text = "A"
Case 79 to 60
lblMaths.text = "B"
Case 59 to 40
lblMaths.text = "C"
Case 39 to 0
lblMaths.text = "D"
Case Else
lblMaths.text = "Invalid Score"
End Select

Ok, don't do a control array, just a normal 2d array will do. Say you have 4 subjects for grades, just do the code
Dim intGrades(4) as integer
Well, you could have "intGrades(3)" because in that there are spots 0-3 but having 1-4 makes it more 'programmer friendly'.

Instead of repeating the code, you can make a new Sub Routine that allows you to access that code with different variables (your array). You could program a button click to call that code but for now, you can just but separate button clicks for each text box and just modifying the code for each button/text box/label

2007-03-13 16:18:42 · answer #2 · answered by saviour 3 · 0 0

I'll point you in the right direction but I won't do it for you.

First, you'll want to take the score and make sure it's valid:

function isScoreValid whichScore
' In here, convert it to a number
' See if it's >= 0 AND <=100
' Better yet, make sure it's an integer (not a floating point)
' If it meets these criteria, isScoreValid = true
' else isScoreValid =false
end

So, you can pass a score from ANY text box to that function.

Next, if it is valid, get the letter score (you could combine these two functions, but for clarity let's keep them separate):

function getLetterGrade score
' Basically test each level
if (score >= 80) then
getLetterGrade ="A"
else if (score >=60) then
getLetterGrade ="B"
'and so on...
end

So first you would check if it was valid, and if it was, get the letter grade. Make sure you take care of a situation where it is NOT valid.

Good luck!

2007-03-13 15:26:43 · answer #3 · answered by T J 6 · 0 1

fedest.com, questions and answers