📄 serializationtestform.vb
字号:
Imports System.io
Imports System.Runtime.Serialization.Formatters.Binary
Public Class TestForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents bttnDeserialize As System.Windows.Forms.Button
Friend WithEvents bttnSerialize As System.Windows.Forms.Button
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.bttnDeserialize = New System.Windows.Forms.Button()
Me.bttnSerialize = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'bttnDeserialize
'
Me.bttnDeserialize.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.bttnDeserialize.Location = New System.Drawing.Point(8, 264)
Me.bttnDeserialize.Name = "bttnDeserialize"
Me.bttnDeserialize.Size = New System.Drawing.Size(312, 40)
Me.bttnDeserialize.TabIndex = 0
Me.bttnDeserialize.Text = "Read && Display Serialized ArrayList"
'
'bttnSerialize
'
Me.bttnSerialize.Font = New System.Drawing.Font("Verdana", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.bttnSerialize.Location = New System.Drawing.Point(8, 216)
Me.bttnSerialize.Name = "bttnSerialize"
Me.bttnSerialize.Size = New System.Drawing.Size(312, 40)
Me.bttnSerialize.TabIndex = 1
Me.bttnSerialize.Text = "Create && Serialize ArrayList"
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.TextBox1.Location = New System.Drawing.Point(8, 8)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(312, 200)
Me.TextBox1.TabIndex = 2
Me.TextBox1.Text = ""
'
'TestForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(328, 309)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1, Me.bttnSerialize, Me.bttnDeserialize})
Me.Name = "TestForm"
Me.Text = "ArrayList Serialization"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Serialize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnSerialize.Click
Dim R1 As New Rectangle()
R1.X = 1
R1.Y = 1
R1.Width = 10
R1.Height = 20
Dim R2 As New Rectangle()
R2.X = 10
R2.Y = 10
R2.Width = 100
R2.Height = 200
Dim Shapes As New ArrayList()
Shapes.Add(R1)
Shapes.Add(R2)
Shapes.Add(Color.Chartreuse)
Shapes.Add(Color.DarkKhaki.GetBrightness)
Shapes.Add(Color.DarkKhaki.GetHue)
Shapes.Add(Color.DarkKhaki.GetSaturation)
Dim saveFile As FileStream
saveFile = File.OpenWrite("C:\SHAPESCOLORS.BIN")
saveFile.Seek(0, SeekOrigin.End)
Dim Formatter As BinaryFormatter = New BinaryFormatter()
Formatter.Serialize(saveFile, Shapes)
saveFile.Close()
MsgBox("ArrayList serialized successfully")
End Sub
Private Sub Deserialize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnDeserialize.Click
Dim readFile As FileStream
readFile = File.OpenRead("c:\SHAPESCOLORS.BIN")
Dim BFormatter As BinaryFormatter
BFormatter = New BinaryFormatter()
Dim Shapes As New ArrayList()
Dim R1 As Rectangle
Shapes = CType(BFormatter.Deserialize(readFile), ArrayList)
Dim i As Integer
TextBox1.AppendText("The ArrayList contains " & Shapes.Count & " objects" & vbCrLf & vbCrLf)
For i = 0 To Shapes.Count - 1
TextBox1.AppendText(Shapes(i).ToString & vbCrLf)
Next
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -