Exercice Visual Basic : Utilisation des collections
Rédigé par GC Team, Publié le 06 Janvier 2012, Mise à jour le Samedi, 17 Décembre 2022 16:35
Participez au vote ☆☆☆☆☆★★★★★
On souhaite maintenant re-développer quelques fonctionnalités de cette application (suite à l'exercice gestion Stagiaire) en Vb (Utilisation des collections)
- Créer un formulaire permettant d’ajouter, modifier, supprimer et rechercher un stagiaire
- Programmer les fonctionnalités suivantes :
- Ajout
- Modification
- Suppression
- Recherche
- Ajouter un formulaire contenant une liste permettant d’afficher les stagiaires et leurs notes ainsi que la moyenne par stagiaire triés par filière.
- Afin d’imprimer cette liste, nous souhaitons enregistrer l’ensemble des enregistrement dans un fichier texte. Ecrire le programme permettant de réaliser cette fonctionnalité
- Protéger l’accès à cette application par :
-
- Compte : Admin
- Mot de passe : Cmoi
Rappel:
Soit la class Stagiaire qui modélise un stagiaire , cette class comportera les attributs suivants :
Matricule INT
Nom String
Prénom String
Filière String
Note1 Double
Note2 Double
Note3 Double
Interface Graphique:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
Public Class Stagiaire Private Matricule As Integer Private Nom As String Private Prénom As String Private Filière As String Private Note1 As Double Private Note2 As Double Private Note3 As Double Public moy As Double Shared compteur As Integer = 1 Property _Matricule() As Integer Get Return Matricule End Get Set(ByVal value As Integer) Matricule = value End Set End Property Property _nom() As String Get Return Nom End Get Set(ByVal value As String) Nom = value End Set End Property Property _Prénom() As String Get Return Prénom End Get Set(ByVal value As String) Prénom = value End Set End Property Property _Filière() As String Get Return Filière End Get Set(ByVal value As String) Filière = value End Set End Property Property _note1() As Double Get Return Note1 End Get Set(ByVal value As Double) Note1 = value End Set End Property Property _note2() As Double Get Return Note2 End Get Set(ByVal value As Double) Note2 = value End Set End Property Property _note3() As Double Get Return Note3 End Get Set(ByVal value As Double) Note3 = value End Set End Property Sub New() Matricule = compteur End Sub Public Sub New(ByVal N As String, ByVal P As String, ByVal F As String) compteur += 1 _Matricule = compteur _nom = N _Prénom = P _Filière = F End Sub Public Sub New(ByVal N As String, ByVal P As String, ByVal F As String, ByVal N1 As Double, ByVal N2 As Double, ByVal N3 As Double) compteur += 1 _Matricule = compteur _nom = N _Prénom = P _Filière = F _note1 = N1 _note2 = N2 _note3 = N3 End Sub Sub RAZ() compteur = 0 End Sub Public Sub EQUAL(ByVal s1 As Stagiaire, ByVal s2 As Stagiaire) If s1.Matricule = s2.Matricule Then Console.WriteLine("Se stagiaire existe déjà") End If End Sub Public Function CALCUL() As Double Return Math.Round((Note1 + Note2 + Note3) / 3, 2) End Function Sub ERRORNOTE() If Note1 > 20 Or Note1 < 0 Then Dim ERRORNOTE As New Exception("Entrer la note1 correctement") ElseIf Note2 > 20 Or Note2 < 0 Then Dim ERRORNOTE As New Exception("Entrer la note2 correctement") ElseIf Note3 > 20 Or Note3 < 0 Then Dim ERRORNOTE As New Exception("Entrer la note3 correctement") End If End Sub Public Sub Afficher() Console.WriteLine(_Matricule & vbTab & _nom & vbTab & _Prénom & vbTab & _Filière) End Sub Public Sub affichage() Console.WriteLine(_Matricule & vbTab & _nom & vbTab & _Prénom & vbTab & _Filière & Note1 & vbTab & Note2 & vbTab & Note3) End Sub End Class ---------------------------------------------------------------------------- Public Class Form1 Dim stag As New Stagiaire Public i, pos, n As Integer Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click stag._Matricule += 1 Txtmat.Text = stag._Matricule txtnom.Text = "" txtprénom.Text = "" txtfil.Text = "" Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click If MsgBox("Voulez vous vraiment quitter l'application", MsgBoxStyle.YesNoCancel, "Confirmation Sortie") = MsgBoxResult.Yes Then End End If End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim stag As New Stagiaire(txtnom.Text, txtprénom.Text, txtfil.Text, Text1.Text, Text2.Text, Text3.Text) stag._Matricule = Txtmat.Text stag._nom = txtnom.Text stag._Prénom = txtprénom.Text stag._Filière = txtfil.Text stag._note1 = Text1.Text stag._note2 = Text2.Text stag._note3 = Text3.Text stag.moy = Text4.Text lst.Add(stag) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Txtmat.Text = stag._Matricule End Sub Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click Text4.Text = Math.Round((CDbl(Text1.Text) + CDbl(Text2.Text) + CDbl(Text3.Text)) / 3, 2) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If MsgBox("Voulez vous vraiment modifier cette enregestrement", MsgBoxStyle.YesNoCancel, "Confirmation Modification") = MsgBoxResult.Yes Then For Each ele In lst If ele._matricule = Txtmat.Text Then Try lst.RemoveAt(i) If lst.Count < 1 Then lst.Add(ele) Else lst.Insert(i, ele) End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Information) End Try Exit For End If i += 1 Next End If End Sub Sub lire(ByVal pos As Integer) stag = lst(i) remplire() End Sub Sub remplire() Txtmat.Text = stag._Matricule txtnom.Text = stag._nom txtprénom.Text = stag._Prénom txtfil.Text = stag._Filière Text1.Text = stag._note1 Text2.Text = stag._note2 Text3.Text = stag._note3 Text4.Text = stag.moy End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click If MsgBox("Voulez vous vraiment Supprimer cette enregestrement", MsgBoxStyle.YesNoCancel, "Confirmation Suppression") = MsgBoxResult.Yes Then For Each elem In lst If elem._matricule = Txtmat.Text Then lst.RemoveAt(i) Try lire(i) pos = i Catch ex As Exception If lst.Count < 1 Then raz() Else lire(i - 1) pos = i - 1 End If End Try Exit For End If i += 1 Next End If End Sub Sub raz() stag._Matricule += 1 Txtmat.Text = stag._Matricule txtnom.Text = "" txtprénom.Text = "" txtfil.Text = "" Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim a As Integer = InputBox("Entrer le num du stagiaire sue vous voulez rechercher", "Recherche") For i = 0 To lst.Count - 1 stag = lst(i) If a = stag._Matricule Then remplire() pos = i Exit Sub End If Next i += 1 MsgBox("Lélément que vous chercher n'existe pas", MsgBoxStyle.Exclamation, "Rechercher") End Sub Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click Affichage.Show() End Sub Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click FileOpen(1, "naziha.txt", OpenMode.Output) For i = 0 To lst.Count - 1 stag = lst(i) PrintLine(1, stag._Matricule & "/" & stag._nom & "/" & stag._Prénom & "/" & stag._Filière & "/" & stag._note1 & "/" & stag._note2 & "/" & stag._note3 & "/" & stag.moy) Next FileClose() End Sub End Class ------------------------------------------------------------------------------- Public Class Affichage Dim lvi As New ListViewItem Dim stag As New Stagiaire Private Sub Affichage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For i = 0 To lst.Count - 1 stag = lst(i) Dim lvi = New ListViewItem(stag._Matricule) lvi.SubItems.Add(stag._nom) lvi.SubItems.Add(stag._Prénom) lvi.SubItems.Add(stag._Filière) lvi.SubItems.Add(stag._note1) lvi.SubItems.Add(stag._note2) lvi.SubItems.Add(stag._note3) lvi.SubItems.Add(stag.moy) ListView1.Items.Add(lvi) ListView1.Select() Next End Sub End Class |
