Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question<Serializable()> _ Public Class Comment ' Private _CommentDate As Date Private _CommentContent As String ' Public Property CommentDate As Date Get Return _CommentDate End Get Set(ByVal value As Date) _CommentDate = value End Set End Property Public Property CommentContent As String Get Return _CommentContent End Get Set(ByVal value As String) _CommentContent = value End Set End Property ' End Class
<Serializable()> _ Public Class CommentCollection Inherits List(Of Comment) 'Ici notre fonction permettant de sauvegarder notre collection dans un fichier Public Sub SaveToFile(ByVal filename As String) ' Dim fs As Stream = Nothing ' Try ' 'Supprime le fichier s'il existe If IO.File.Exists(filename) Then IO.File.Delete(filename) End If ' 'Ouvre un stream et sérialise le contenu de notre classe dans celui-ci fs = File.Create(filename) Dim serializer As New BinaryFormatter serializer.Serialize(fs, Me) ' Catch ex As Exception ' 'En cas d'erreur MessageBox.Show("Une erreur d'enregistrement s'est produite !", _ "Echec d'enregistrement", _ MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation) ' Finally ' 'On referme le fichier dans tous les cas If fs IsNot Nothing Then fs.Close() fs.Dispose() End If ' End Try ' End Sub Public Shared Function LoadFromFile(ByVal filename As String) As CommentCollection ' Dim fs As Stream = Nothing ' Try ' 'Ici si le fichier n'existe pas c'est probablement que le logiciel n'a jamais été utilisé 'On renvoi donc une nouvelle instance vide de notre objet If Not IO.File.Exists(filename) Then ' Return New CommentCollection ' Else ' 'Dans le cas contraire on dé-sérialise, on renvoi une collection chargé d'après le 'fichier enregistré précédemment fs = File.OpenRead(filename) Dim deserializer As New BinaryFormatter() Return DirectCast(deserializer.Deserialize(fs), CommentCollection) ' End If ' Catch ex As Exception ' 'En cas d'erreur MessageBox.Show("Une erreur est survenue durant la lecture du fichier !", _ "Echec de lecture des informations", _ MessageBoxButtons.OK, _ MessageBoxIcon.Exclamation) Return Nothing ' Finally ' If fs IsNot Nothing Then fs.Close() fs.Dispose() End If ' End Try ' End Function 'Cette fonction nous permet de retrouver le commentaire lié à une date Public Function FindByDate(ByVal value As Date) As Comment ' For Each Item As Comment In Me If Item.CommentDate = value Then Return Item End If Next ' Return Nothing ' End Function ' End Class
Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary
Public Class Form1 ' 'Emplacement du fichier sérialisé Dim DataFile As String = "comments.bin" 'Notre objet instancié d'après le fichier (dé-sérialisation) Dim Comments As CommentCollection = CommentCollection.LoadFromFile(DataFile) ' Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged ' 'On vide le TextBox du commentaire txt_Comment.Text = "" 'Si le commentaire existe dans notre collection (retrouvé d'après sa date) on l'affiche Dim CurrentComment As Comment = Comments.FindByDate(DateTimePicker1.Value.ToShortDateString) If CurrentComment IsNot Nothing Then _ txt_Comment.Text = CurrentComment.CommentContent ' End Sub ' Private Sub Button_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Save.Click ' 'On recherche si le commentaire existe d'après la date Dim CurrentComment As Comment = Comments.FindByDate(DateTimePicker1.Value.ToShortDateString) 'Si il existe on le modifie If CurrentComment IsNot Nothing Then CurrentComment.CommentContent = txt_Comment.Text Comments.SaveToFile(DataFile) Else 'Dans le cas contraire on l'ajoute à notre collection Dim newComment As New Comment With newComment .CommentDate = DateTimePicker1.Value.ToShortDateString .CommentContent = txt_Comment.Text End With Comments.Add(newComment) Comments.SaveToFile(DataFile) End If 'Dans les deux cas on sauvergarde (Ecrasement de l'ancien fichier) ' End Sub ' End Class