Je suis nouveau sur VB, j'ai reçu une mission de mon professeur pour créer une calculatrice CGPA. Voici le formulaire. Comme vous pouvez le voir, j'essaie d'obtenir les valeurs des «heures de crédit», puis de les résumer et de les afficher sur «total ch». Comment faire? Ces valeurs proviennent de différents sous privés, elles ne sont donc pas disponibles globalement. Quelqu'un peut-il m'expliquer comment puis-je obtenir les valeurs? Ou peut-être y a-t-il un moyen meilleur / plus simple de le faire?

Voici mes codes

Public Class Form1

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid0.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid0.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid0.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid0.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname0.Text = coursename
    txtch0.Text = credithour
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid1.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid1.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid1.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid1.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname1.Text = coursename
    txtch1.Text = credithour
End Sub

Private Sub TextBox10_TextChanged(sender As Object, e As EventArgs) Handles txtcname2.TextChanged

End Sub

Private Sub TextBox8_TextChanged(sender As Object, e As EventArgs) Handles txtcname0.TextChanged

End Sub

Private Sub cbocid2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid2.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid2.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid2.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid2.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname2.Text = coursename
    txtch2.Text = credithour
End Sub

Private Sub cbocid3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid3.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid3.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid3.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid3.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname3.Text = coursename
    txtch3.Text = credithour
End Sub

Private Sub cbocid4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid4.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid4.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid4.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid4.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname4.Text = coursename
    txtch4.Text = credithour
End Sub

Private Sub cbocid5_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbocid5.SelectedIndexChanged
    Dim coursename As String
    Dim credithour As Integer

    If cbocid5.SelectedIndex = 0 Then
        coursename = "Computer System & Applications"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 1 Then
        coursename = "Discrete Structures"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 2 Then
        coursename = "Program Design"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 3 Then
        coursename = "Mathematical Techniques 1"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 4 Then
        coursename = "Mathematical Techniques 2"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 5 Then
        coursename = "Database Systems"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 6 Then
        coursename = "Contemporary Management & Entrepreneurship"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 7 Then
        coursename = "English"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 8 Then
        coursename = "Business Management"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 9 Then
        coursename = "Computer Architecture"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 10 Then
        coursename = "Operating Systems"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 11 Then
        coursename = "Systems Analysis & Design"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 12 Then
        coursename = "Data Communications & Networking"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 13 Then
        coursename = "Internet & Web Publishing"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 14 Then
        coursename = "Object Oriented Programming"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 15 Then
        coursename = "Programming In Java"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 16 Then
        coursename = "Data Structure & Algorithms"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 17 Then
        coursename = "Computing Project"
        credithour = "4"
    ElseIf cbocid5.SelectedIndex = 18 Then
        coursename = "Effective Communication Skills"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 19 Then
        coursename = "E-Commerce"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 20 Then
        coursename = "Multimedia Applications"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 21 Then
        coursename = "Management Information System"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 22 Then
        coursename = "Programming For Business Applications"
        credithour = "3"
    ElseIf cbocid5.SelectedIndex = 23 Then
        coursename = "Industrial Training"
        credithour = "6"
    End If
    txtcname5.Text = coursename
    txtch5.Text = credithour
End Sub

Sub ClearAll()
    cbocid0.SelectedIndex = 24
    cbocid1.SelectedIndex = 24
    cbocid2.SelectedIndex = 24
    cbocid3.SelectedIndex = 24
    cbocid4.SelectedIndex = 24
    cbocid5.SelectedIndex = 24
End Sub

Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub txttotalch_TextChanged(sender As Object, e As EventArgs) Handles txttotalch.TextChanged

End Sub

Private Sub btncal_Click(sender As Object, e As EventArgs) Handles btncal.Click

End Sub

Private Sub txtch0_TextChanged(sender As Object, e As EventArgs) Handles txtch0.TextChanged

End Sub

Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click
    ClearAll()
End Sub

Fin de classe

-2
Bong Meister 5 janv. 2016 à 14:22

2 réponses

Meilleure réponse

Tout d'abord, déplacez vos variables du sous-marin vers le haut sous la classe pour les rendre globales. Vous n'avez qu'une seule variable pour l'heure de crédit (crédit), donc chaque fois que vous modifiez une valeur dans l'une des zones de liste déroulante, vous ne modifiez qu'une seule valeur. Définissez une variable pour chacun des nombres de valeurs d'heures de crédit dont vous avez besoin et créez une variable pour le total dim totalCH as integer

Puis à la fin de chaque procédure, tapez simplement quelque chose comme totalCH = credithour1 + credithour2...

0
Arazio 5 janv. 2016 à 11:33

Je vous recommande de créer un attribut de niveau classe qui est un dictionnaire de tuples. Où la clé est l'index et la valeur est un tuple du nom du cours et de l'heure de crédit. Ensuite, toutes vos fonctions peuvent arrêter de dupliquer autant de code et simplement extraire du dictionnaire. Vous devrez initialiser votre dictionnaire dans votre constructeur.

0
Nathan C 5 janv. 2016 à 13:25