Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
'We save our data by placing the data
'into this object
Dim info As New PersonInfo
Dim bf As New BinaryFormatter
'Create or trucate a file to save the data and opens
'it for writing
Dim fs As New FileStream("c:\PersonInfo.bin", FileMode.Create, FileAccess.Write)
'We put our information from the TextBoxes
'into the PersonInfo object
info.Name = txtName.Text
info.Age = nudAge.Value
info.Address = txtAddr.Text
'Writes the data into the FileStream using the object
bf.Serialize(fs, info)
'Writes the information to the file on disk
'and closes the stream
fs.Close()
End Sub
Private Sub btnLoad_Click(sender As System.Object, e As System.EventArgs) Handles btnLoad.Click
'Open the file
Dim fs As New FileStream("C:\PersonInfo.bin", FileMode.OpenOrCreate, FileAccess.Read)
Dim bf As New BinaryFormatter
'Get the information from the file which
'would be placed into a PersonInfo object
Dim info As PersonInfo = DirectCast(bf.Deserialize(fs), PersonInfo)
'Put the data in the PersonInfo object
'into the TextBoxes
txtAddr.Text = info.Address
txtName.Text = info.Name
nudAge.Value = info.Age
'Close the file
fs.Close()
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.txtName = New System.Windows.Forms.TextBox()
Me.nudAge = New System.Windows.Forms.NumericUpDown()
Me.txtAddr = New System.Windows.Forms.TextBox()
Me.btnSave = New System.Windows.Forms.Button()
Me.btnLoad = New System.Windows.Forms.Button()
CType(Me.nudAge, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(35, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Name"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(12, 100)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(45, 13)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Address"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(12, 56)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(26, 13)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Age"
'
'txtName
'
Me.txtName.Location = New System.Drawing.Point(63, 6)
Me.txtName.Name = "txtName"
Me.txtName.Size = New System.Drawing.Size(195, 20)
Me.txtName.TabIndex = 3
'
'nudAge
'
Me.nudAge.Location = New System.Drawing.Point(63, 54)
Me.nudAge.Name = "nudAge"
Me.nudAge.Size = New System.Drawing.Size(70, 20)
Me.nudAge.TabIndex = 4
'
'txtAddr
'
Me.txtAddr.Location = New System.Drawing.Point(63, 97)
Me.txtAddr.Name = "txtAddr"
Me.txtAddr.Size = New System.Drawing.Size(195, 20)
Me.txtAddr.TabIndex = 5
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(15, 137)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(86, 32)
Me.btnSave.TabIndex = 6
Me.btnSave.Text = "Save"
Me.btnSave.UseVisualStyleBackColor = True
'
'btnLoad
'
Me.btnLoad.Location = New System.Drawing.Point(107, 137)
Me.btnLoad.Name = "btnLoad"
Me.btnLoad.Size = New System.Drawing.Size(86, 32)
Me.btnLoad.TabIndex = 7
Me.btnLoad.Text = "Load"
Me.btnLoad.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(297, 181)
Me.Controls.Add(Me.btnLoad)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtAddr)
Me.Controls.Add(Me.nudAge)
Me.Controls.Add(Me.txtName)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.nudAge, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtName As System.Windows.Forms.TextBox
Friend WithEvents nudAge As System.Windows.Forms.NumericUpDown
Friend WithEvents txtAddr As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents btnLoad As System.Windows.Forms.Button
End Class
'The Serializable attribute marks a file as
'serializable
<Serializable()> _
Public Class PersonInfo
Public Property Name As String
Public Property Address As String
Public Property Age As Integer
End Class