0
我已經設計了一個GPA計算器,它們分別是8個文本框輸入課程名稱,8個文本框每個課程的小時數值,最後8個文本框用於您在這些課程中的字母等級。我需要花費信用時間的8個文本框出現問題。如果我不覺得每個人都有一個號碼,它會給我錯誤「輸入字符串格式不正確」。我想將它編程到如果用戶只需要5個課程,他們可以將其他3個文本框留空,並且在他們沒有填入信用時間的文本框中輸入「0」,並且只需計算GPA輸入了5個班級。如果用戶輸入信用時間的所有8個文本框,它會正常工作,但是,如果上面提到的錯誤代碼出現,如果他們不填寫全部8個。GPA計算器文本框問題
Public Class Form1
Dim QualityPoints As Double
Dim CourseName, hours As String
Private Sub btnTotalHours_Click(sender As Object, e As EventArgs) Handles btnTotalHours.Click
Dim totalHours As String
Dim Credit1, Credit2, Credit3, Credit4, Credit5, Credit6, Credit7, Credit8 As Integer
Credit1 = Integer.Parse(txtBoxCredit1.Text)
Credit2 = Integer.Parse(txtBoxCredit2.Text)
Credit3 = Integer.Parse(txtBoxCredit3.Text)
Credit4 = Integer.Parse(txtBoxCredit4.Text)
Credit5 = Integer.Parse(txtBoxCredit5.Text)
Credit6 = Integer.Parse(txtBoxCredit6.Text)
Credit7 = Integer.Parse(txtBoxCredit7.Text)
Credit8 = Integer.Parse(txtBoxCredit8.Text)
totalHours = CStr(txtBoxTotalHours.Text)
txtBoxTotalHours.Text = (Credit1 + Credit2 + Credit3 + Credit4 + Credit5 + Credit6 + Credit7 + Credit8)
End Sub
Private Sub btnTotalQualityPoints_Click(sender As Object, e As EventArgs) Handles btnTotalQualityPoints.Click
Select Case txtBoxGrade1.Text
Case "A"
QualityPoints += (4 * txtBoxCredit1.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit1.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit1.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit1.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit1.Text)
Case " "
QualityPoints += (0 * txtBoxCredit1.Text)
End Select
Select Case txtBoxGrade2.Text
Case "A"
QualityPoints += (4 * txtBoxCredit2.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit2.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit2.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit2.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit2.Text)
Case " "
QualityPoints += (0 * txtBoxCredit2.Text)
End Select
Select Case txtBoxGrade3.Text
Case "A"
QualityPoints += (4 * txtBoxCredit3.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit3.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit3.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit3.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit3.Text)
Case " "
QualityPoints += (0 * txtBoxCredit3.Text)
End Select
Select Case txtBoxGrade4.Text
Case "A"
QualityPoints += (4 * txtBoxCredit4.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit4.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit4.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit4.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit4.Text)
Case " "
QualityPoints += (0 * txtBoxCredit4.Text)
End Select
Select Case txtBoxGrade5.Text
Case "A"
QualityPoints += (4 * txtBoxCredit5.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit5.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit5.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit5.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit5.Text)
Case " "
QualityPoints += (0 * txtBoxCredit5.Text)
End Select
Select Case txtBoxGrade6.Text
Case "A"
QualityPoints += (4 * txtBoxCredit6.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit6.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit6.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit6.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit6.Text)
Case " "
QualityPoints += (0 * txtBoxCredit6.Text)
End Select
Select Case txtBoxGrade7.Text
Case "A"
QualityPoints += (4 * txtBoxCredit7.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit7.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit7.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit7.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit7.Text)
Case " "
QualityPoints += (0 * txtBoxCredit7.Text)
End Select
Select Case txtBoxGrade8.Text
Case "A"
QualityPoints += (4 * txtBoxCredit8.Text)
Case "B"
QualityPoints += (3 * txtBoxCredit8.Text)
Case "C"
QualityPoints += (2 * txtBoxCredit8.Text)
Case "D"
QualityPoints += (1 * txtBoxCredit8.Text)
Case "F"
QualityPoints += (0 * txtBoxCredit8.Text)
Case " "
QualityPoints += (0 * txtBoxCredit8.Text)
End Select
txtBoxTotalQualityPoints.Text = QualityPoints
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtBoxCourse1.Clear()
txtBoxCourse2.Clear()
txtBoxCourse3.Clear()
txtBoxCourse4.Clear()
txtBoxCourse5.Clear()
txtBoxCourse6.Clear()
txtBoxCourse7.Clear()
txtBoxCourse8.Clear()
txtBoxCredit1.Clear()
txtBoxCredit2.Clear()
txtBoxCredit3.Clear()
txtBoxCredit4.Clear()
txtBoxCredit5.Clear()
txtBoxCredit6.Clear()
txtBoxCredit7.Clear()
txtBoxCredit8.Clear()
txtBoxGrade1.Clear()
txtBoxGrade2.Clear()
txtBoxGrade3.Clear()
txtBoxGrade4.Clear()
txtBoxGrade5.Clear()
txtBoxGrade6.Clear()
txtBoxGrade7.Clear()
txtBoxGrade8.Clear()
txtBoxTotalGPA.Clear()
txtBoxTotalHours.Clear()
txtBoxTotalQualityPoints.Clear()
End Sub
感謝您提供的任何幫助!
所以我應該改變「Integer.Parse」到「int32.TryParse」,然後寫正確的if語句? – user2213611 2013-03-27 19:16:24
你能否提供一個你正在談論的小例子? – user2213611 2013-03-27 19:39:00
我修好了,最後用「numericUpDown」 – user2213611 2013-03-27 20:14:49