Eine Klasse für die Serialisierung von Meldungen:
<Serializable()> _
Public Class Spion
Implements IDisposable
<Serializable()> _
Public Class SpionItem
Public ReadOnly ComboItem As String
Public ReadOnly DatumZeit As DateTime
'weitere Info-Properties z.B. Benutzer
Public Sub New(ComboText As String, dt As DateTime)
ComboItem = ComboText
DatumZeit = dt
End Sub
End Class
Private SpionDatei As String
Private ReadOnly SpionItemList As New List(Of SpionItem)
<NonSerialized()> _
Dim fmt As New System.Runtime.Serialization.Formatters. _
Binary.BinaryFormatter()
Public Sub New(SpionDateiPfad As String)
If IO.File.Exists(SpionDateiPfad) Then
Using fs As New IO.FileStream(SpionDateiPfad, _
IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
Dim cs As Spion = DirectCast(fmt.Deserialize(fs), Spion)
Me.SpionItemList = cs.SpionItemList
End Using
End If
SpionDatei = SpionDateiPfad
End Sub
Public Sub addItem(Combotext As String, dt As DateTime)
SpionItemList.Add(New SpionItem(Combotext, dt))
End Sub
Public ReadOnly Property GetList As DataTable
Get
Dim dt As New DataTable
dt.Columns.Add("ComboItem") : dt.Columns.Add("DatumZeit")
With SpionItemList
For i As Integer = 0 To .Count - 1
dt.Rows.Add(.Item(i).ComboItem, .Item(i).DatumZeit)
Next i
End With
Return dt
End Get
End Property
Public Sub Serialize()
IO.File.Delete(SpionDatei)
Try
Using fs As New IO.FileStream(SpionDatei, _
IO.FileMode.Create, IO.FileAccess.Write, IO.FileShare.None)
fmt.Serialize(fs, Me)
End Using
Catch ex As Exception
Stop
End Try
End Sub
Private disposedValue As Boolean
Protected Overridable Sub Dispose(disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Serialize()
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class Einbau der Klasse in eine Combobox-Ableitung:
Public Class ComboBoxSpion
Inherits ComboBox
Dim sp As New Spion("C:\Daten\ComboboxSpionage.ser")
Protected Overrides Sub OnSelectedIndexChanged(e As System.EventArgs)
MyBase.OnSelectedIndexChanged(e)
sp.addItem(DirectCast(Me.Items(Me.SelectedIndex), String), Now)
End Sub
Public ReadOnly Property GetSpionItems() As DataTable
Get
Return sp.GetList
End Get
End Property
Private Sub ComboBoxSpion_Disposed(sender As Object, _
e As System.EventArgs) Handles Me.Disposed
sp.Serialize()
End Sub
End Class Formular: Anwendungsbeispiel
Public Class frmComboBoxSpion
Dim cbo As New ComboBoxSpion With _
{.Parent = Me, .Width = 200, .Height = 50}
Dim dt As DataTable
Dim bs As New BindingSource
Dim dgv As New DataGridView With _
{.Parent = Me, .Width = 200, .Height = 200, _
.Top = 110, .DataSource = bs}
Dim WithEvents btn As New Button With _
{.Parent = Me, .Width = 200, .Top = 80, _
.Text = "Items zeigen"}
Private Sub frmComboBoxSpion_Load(sender As System.Object, _
e As System.EventArgs) Handles MyBase.Load
cbo.Items.AddRange({"Projekt Müller", "Angebot Meier", _
"Rechnung Schröder", "Projekt Buchs"})
End Sub
Private Sub btn_Click(sender As Object, _
e As System.EventArgs) Handles btn.Click
bs.DataSource = cbo.GetSpionItems
End Sub
End Class |