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
| Private Sub btnEnregistre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnregistre.Click
dicoTextBox.Clear()
NoteTextBox(Me, dicoTextBox)
SaveDico(dicoTextBox)
End Sub
''' <summary>Stoque toutes les données des textBox dans un dictionnaire.</summary>
''' <param name="conteneur">un conteneur de textbox (form, panel, groupbox)</param>
''' <param name="UnDico">le dictionnaire contenant => clé : "nom textbox", valeur : "texte du textbox".</param>
''' <remarks></remarks>
Sub NoteTextBox(ByVal conteneur As Control, ByRef UnDico As Dictionary(Of String, String))
For Each ctrl As Control In conteneur.Controls
'select sur true (condition vrai), qui sera vérifié sur chaque case si "ctrl" correspond au type de controle
Select Case True
Case TypeOf ctrl Is TextBox
UnDico.Add(ctrl.Name, ctrl.Text)
Case TypeOf ctrl Is GroupBox
NoteTextBox(ctrl, UnDico)
Case TypeOf ctrl Is Panel
NoteTextBox(ctrl, UnDico)
End Select
Next
End Sub
Sub SaveDico(ByVal UnDico As Dictionary(Of String, String))
Dim savefile As New SaveFileDialog
savefile.InitialDirectory = "D:\Projets\Data\"
savefile.DefaultExt = ".txt"
savefile.FileName = "fichierCfg"
savefile.Filter = "fichier de configuration (*.txt)|*.txt"
savefile.ShowDialog()
Try
Dim ecrire As New System.IO.StreamWriter(savefile.FileName)
For Each element As KeyValuePair(Of String, String) In UnDico
ecrire.WriteLine("{0}|{1}", element.Key, element.Value)
Next element
ecrire.Close()
Catch ex As Exception
End Try
End Sub |
Partager