Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am new to vb and creating a gpa calculator using 10 combo boxes to select grades (each grades have various points) and 10 text boxes to enter credits
i want these credits to be multiplied by the grade points in front of it and get the total of grade points to be devided by the total credits
for this purpose i have created these functions
VB
Module Module1
    Public Function Grade(ByVal g) As Single
        Select Case g
            Case "A+"
                Grade = 4.2
            Case "A"
                Grade = 4
            Case "A-"
                Grade = 3.7
            Case "B+"
                Grade = 3.3
            Case "B"
                Grade = 3
            Case "B-"
                Grade = 2.7
            Case "C+"
                Grade = 2.3
            Case "C"
                Grade = 2
            Case "C-"
                Grade = 1.5
            Case "D"
                Grade = 1
            Case Else
                Grade = 0
        End Select
    End Function
    Public Function Color(ByVal c) As Color
        Select Case c
            Case "A+", "A", "A-", "B+"
                Color = Color.Green
            Case "B", "B-", "C+"
                Color = Color.YellowGreen
            Case "C", "C-"
                Color = Color.Yellow
            Case "D", "I", "F"
                Color = Color.Red
        End Select
    End Function
End Module

now i want to call these function for 10 times using a for loop and the answer will be displayed in the lable
this is the code to call the function but it doesn't success can any one help me to do the necessaries
thanks in advance


VB
Dim CreditPoints, Tcredits As single
Tcredits = 0
        CreditPoints = 0
For i As Integer = 1 To 10 Step 1
Dim gr As Single = 0
        gr = Grade(CmbGrade1(i).SelectedItem)
        CreditPoints += gr * Val(TxtCredit1(i).Text)
        Tcredits += Val(TxtCredit1(i).Text)
next i
    LblGPA.Text = CreditPoints / Tcredits
    End Sub
Posted

I don't complete understand your 'Problem'.
But I see 2 possible solutions for you.

1st :
You build up a UserControl inside which the ComboBox and the corresponding Textbox are integrated.
Inside the Script of the UserControl you catch the 'SelectedIndexChanged' from the Combobox and do the Textbox-assign at this place.

2nd :
You customize your ComboBox - create a new class which inherits the Combobox.
Inside this class you also catch the 'SelectedIndexChanged' and do the nescessary calculation inside this class. The result of this calculation will be delivered by a new Property (for example 'SelectedGrade' of type Single) to outside the/your customized Combobox.
 
Share this answer
 
You can replace your code by this :
VB
Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click
    Dim CreditPoints, Tcredits As Single
    Tcredits = 0
    CreditPoints = 0
    Dim TotalGrade(10) As Single

    Dim a As Control
    For Each a In Me.Controls
        If TypeOf a Is ComboBox Then
            If a.Name.Contains("CmbGrade") Then
                TotalGrade(CInt(a.Name.Remove("CmbGrade")) - 1) = Grade(DirectCast(a, ComboBox).SelectedItem)

            End If

        End If
    Next
    For Each a In Me.Controls
        If TypeOf a Is TextBox Then
            If a.Name.Contains("TxtCredit") Then
                TotalGrade(CInt(a.Name.Remove("TxtCredit")) - 1) *= Val(DirectCast(a, TextBox).Text)
                Tcredits = Tcredits + Val(Val(DirectCast(a, TextBox).Text))
            End If
        End If
    Next


    CreditPoints = TotalGrade.Cast(Of Double).Sum()

    LblGPA.Text = CreditPoints / Tcredits
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
    Dim a As Control
    For Each a In Me.Controls
        If TypeOf a Is TextBox Or TypeOf a Is ComboBox Then
            a.Text = Nothing
        End If
    Next
End Sub


Private Sub Check_Text_And_Color(ByVal txtBxName As TextBox, ByRef CmbGrade As ComboBox)
    If Not IsNumeric(txtBxName.Text) And Not (txtBxName.Text) = Nothing Then
        txtBxName.Clear()
        MsgBox("Please enter a number!")
    Else
        txtBxName.BackColor = Color(CmbGrade.SelectedItem)
    End If
End Sub
Private Sub TxtCredit1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit1.TextChanged
    Check_Text_And_Color(TxtCredit1, CmbGrade1)
End Sub
Private Sub TxtCredit2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit2.TextChanged
    Check_Text_And_Color(TxtCredit2, CmbGrade2)
End Sub
Private Sub TxtCredit3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit3.TextChanged
    Check_Text_And_Color(TxtCredit3, CmbGrade3)
End Sub
Private Sub TxtCredit4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit4.TextChanged
    Check_Text_And_Color(TxtCredit4, CmbGrade4)
End Sub
Private Sub TxtCredit5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit5.TextChanged
    Check_Text_And_Color(TxtCredit5, CmbGrade5)
End Sub
Private Sub TxtCredit6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit6.TextChanged
    Check_Text_And_Color(TxtCredit6.Text, CmbGrade6) ' and so on
End Sub
 
Share this answer
 
Where you will use the combobox ? ,
below code to use a for loop to reach all comboboxes on same form :
VB
    For Each GroupBoxCntrol As Control In Me.Controls
        If TypeOf GroupBoxCntrol Is GroupBox Then
            For Each cntrl As Control In GroupBoxCntrol.Controls
                'do somethin here
'cntrl is a combo box 
            Next
        End If

    Next
 
Share this answer
 
Comments
Shambavi Arudchelvan 31-May-15 7:10am    
i use combo boxes to select the grades A+, A, A-, B+,B ...... and if cmbGrade1.selecteditem=A+ Grade1=4.2 or A Grade1=4 ......
but i don't want to apply the same at once i want to repeat the same function for a combination of a combo box(cmbGrade) and a text box(TxtCredit) i.e ((Grade1*txtcredit1.text)+(Grade2*txtcredit2.text)+....)/(txtcredit1.text+txtcredit2.text+txtcredit3.text)
besthms 1-Jun-15 5:13am    
Can you share the full calculation function here ?
Shambavi Arudchelvan 1-Jun-15 9:36am    
<pre>
Public Class FrmGPACalcculator
Private Sub BtnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalculate.Click
Dim CreditPoints, Tcredits As Single
Tcredits = 0
CreditPoints = 0
Dim Gr(10) As Single
Gr(0) = Grade(CmbGrade1.SelectedItem) * Val(TxtCredit1.Text)
Gr(1) = Grade(CmbGrade2.SelectedItem) * Val(TxtCredit2.Text)
Gr(2) = Grade(CmbGrade3.SelectedItem) * Val(TxtCredit3.Text)
Gr(3) = Grade(CmbGrade4.SelectedItem) * Val(TxtCredit4.Text)
Gr(4) = Grade(CmbGrade5.SelectedItem) * Val(TxtCredit5.Text)
Gr(5) = Grade(CmbGrade6.SelectedItem) * Val(TxtCredit6.Text)
Gr(6) = Grade(CmbGrade7.SelectedItem) * Val(TxtCredit7.Text)
Gr(7) = Grade(CmbGrade8.SelectedItem) * Val(TxtCredit8.Text)
Gr(8) = Grade(CmbGrade9.SelectedItem) * Val(TxtCredit9.Text)
Gr(9) = Grade(CmbGrade10.SelectedItem) * Val(TxtCredit10.Text)
Tcredits = Val(TxtCredit1.Text) + Val(TxtCredit2.Text) + Val(TxtCredit3.Text) + Val(TxtCredit4.Text) + Val(TxtCredit5.Text) + Val(TxtCredit6.Text) + Val(TxtCredit7.Text) + Val(TxtCredit8.Text) + Val(TxtCredit9.Text) + Val(TxtCredit10.Text)
For i As Integer = 0 To 9 Step 1
CreditPoints += Gr(i)
Next i
LblGPA.Text = CreditPoints / Tcredits
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
Dim a As Control
For Each a In Me.Controls
If TypeOf a Is TextBox Or TypeOf a Is ComboBox Then
a.Text = Nothing
End If
Next
End Sub
Private Sub TxtCredit1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit1.TextChanged
If Not IsNumeric(TxtCredit1.Text) And Not (TxtCredit1.Text) = Nothing Then
TxtCredit1.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit1.BackColor = Color(CmbGrade1.SelectedItem)
End If
End Sub
Private Sub TxtCredit2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit2.TextChanged
If Not IsNumeric(TxtCredit2.Text) And Not (TxtCredit2.Text) = Nothing Then
TxtCredit2.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit2.BackColor = Color(CmbGrade2.SelectedItem)
End If
End Sub
Private Sub TxtCredit3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit3.TextChanged
If Not IsNumeric(TxtCredit3.Text) And Not (TxtCredit3.Text) = Nothing Then
TxtCredit3.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit3.BackColor = Color(CmbGrade3.SelectedItem)
End If
End Sub
Private Sub TxtCredit4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit4.TextChanged
If Not IsNumeric(TxtCredit4.Text) And Not (TxtCredit4.Text) = Nothing Then
TxtCredit4.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit4.BackColor = Color(CmbGrade4.SelectedItem)
End If
End Sub
Private Sub TxtCredit5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit5.TextChanged
If Not IsNumeric(TxtCredit5.Text) And Not (TxtCredit5.Text) = Nothing Then
TxtCredit5.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit5.BackColor = Color(CmbGrade5.SelectedItem)
End If
End Sub
Private Sub TxtCredit6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtCredit6.TextChanged
If Not IsNumeric(TxtCredit6.Text) And Not (TxtCredit6.Text) = Nothing Then
TxtCredit6.Clear()
MsgBox("Please enter a number!")
Else
TxtCredit6.BackColor = Color(Cmb

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900