File: SnapshotofWindowsFormUsingSerializedHashtable.VB.Txt Date: 7/29/04 By: Dan Garlen, MCT MCT04@Garlen.Net --------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------- Imports System.IO Imports System.Runtime.Serialization.Formatters Public Class Demo 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 '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. 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 txtDoNotSave As System.Windows.Forms.TextBox Friend WithEvents dtpDOB As System.Windows.Forms.DateTimePicker Friend WithEvents cbState As System.Windows.Forms.ComboBox Friend WithEvents btnSave As System.Windows.Forms.Button Friend WithEvents btnRecall As System.Windows.Forms.Button Friend WithEvents ListBox1 As System.Windows.Forms.ListBox Friend WithEvents optMale As System.Windows.Forms.RadioButton Friend WithEvents optFemale As System.Windows.Forms.RadioButton Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents btnResetUserInput As System.Windows.Forms.Button Friend WithEvents lblBefore As System.Windows.Forms.Label Friend WithEvents lblAfter As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label 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.txtDoNotSave = New System.Windows.Forms.TextBox Me.dtpDOB = New System.Windows.Forms.DateTimePicker Me.cbState = New System.Windows.Forms.ComboBox Me.btnSave = New System.Windows.Forms.Button Me.btnRecall = New System.Windows.Forms.Button Me.ListBox1 = New System.Windows.Forms.ListBox Me.optMale = New System.Windows.Forms.RadioButton Me.optFemale = New System.Windows.Forms.RadioButton Me.Label4 = New System.Windows.Forms.Label Me.btnResetUserInput = New System.Windows.Forms.Button Me.lblBefore = New System.Windows.Forms.Label Me.lblAfter = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.SuspendLayout() ' 'Label1 ' Me.Label1.Location = New System.Drawing.Point(16, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(48, 23) Me.Label1.TabIndex = 0 Me.Label1.Text = "Name" ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(16, 48) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(48, 23) Me.Label2.TabIndex = 1 Me.Label2.Text = "DOB" ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(16, 192) Me.Label3.Name = "Label3" Me.Label3.TabIndex = 2 Me.Label3.Text = "State" ' 'txtName ' Me.txtName.Location = New System.Drawing.Point(72, 16) Me.txtName.Name = "txtName" Me.txtName.TabIndex = 0 Me.txtName.Tag = "SAVE" Me.txtName.Text = "" ' 'txtDoNotSave ' Me.txtDoNotSave.Location = New System.Drawing.Point(176, 192) Me.txtDoNotSave.Name = "txtDoNotSave" Me.txtDoNotSave.TabIndex = 5 Me.txtDoNotSave.Text = "Junk" ' 'dtpDOB ' Me.dtpDOB.Format = System.Windows.Forms.DateTimePickerFormat.Short Me.dtpDOB.Location = New System.Drawing.Point(72, 48) Me.dtpDOB.Name = "dtpDOB" Me.dtpDOB.Size = New System.Drawing.Size(88, 20) Me.dtpDOB.TabIndex = 1 Me.dtpDOB.Tag = "SAVE" ' 'cbState ' Me.cbState.Items.AddRange(New Object() {"NJ", "NY", "PA", "CT"}) Me.cbState.Location = New System.Drawing.Point(88, 192) Me.cbState.Name = "cbState" Me.cbState.Size = New System.Drawing.Size(72, 21) Me.cbState.TabIndex = 4 Me.cbState.Tag = "SAVE" Me.cbState.Text = "State" ' 'btnSave ' Me.btnSave.Location = New System.Drawing.Point(32, 232) Me.btnSave.Name = "btnSave" Me.btnSave.TabIndex = 6 Me.btnSave.Text = "Save" ' 'btnRecall ' Me.btnRecall.Location = New System.Drawing.Point(152, 232) Me.btnRecall.Name = "btnRecall" Me.btnRecall.TabIndex = 7 Me.btnRecall.Text = "Recall" ' 'ListBox1 ' Me.ListBox1.Location = New System.Drawing.Point(144, 80) Me.ListBox1.Name = "ListBox1" Me.ListBox1.Size = New System.Drawing.Size(320, 95) Me.ListBox1.TabIndex = 12 ' 'optMale ' Me.optMale.BackColor = System.Drawing.Color.White Me.optMale.Location = New System.Drawing.Point(48, 133) Me.optMale.Name = "optMale" Me.optMale.Size = New System.Drawing.Size(64, 24) Me.optMale.TabIndex = 3 Me.optMale.Tag = "SAVE" Me.optMale.Text = "Male" ' 'optFemale ' Me.optFemale.BackColor = System.Drawing.Color.White Me.optFemale.Location = New System.Drawing.Point(48, 109) Me.optFemale.Name = "optFemale" Me.optFemale.Size = New System.Drawing.Size(64, 24) Me.optFemale.TabIndex = 2 Me.optFemale.Tag = "SAVE" Me.optFemale.Text = "Female" ' 'Label4 ' Me.Label4.Location = New System.Drawing.Point(24, 88) Me.Label4.Name = "Label4" Me.Label4.TabIndex = 15 Me.Label4.Text = "Gender" ' 'btnResetUserInput ' Me.btnResetUserInput.Location = New System.Drawing.Point(368, 232) Me.btnResetUserInput.Name = "btnResetUserInput" Me.btnResetUserInput.Size = New System.Drawing.Size(112, 23) Me.btnResetUserInput.TabIndex = 8 Me.btnResetUserInput.Text = "Reset User Input" ' 'lblBefore ' Me.lblBefore.Location = New System.Drawing.Point(208, 16) Me.lblBefore.Name = "lblBefore" Me.lblBefore.Size = New System.Drawing.Size(64, 23) Me.lblBefore.TabIndex = 18 ' 'lblAfter ' Me.lblAfter.Location = New System.Drawing.Point(352, 16) Me.lblAfter.Name = "lblAfter" Me.lblAfter.Size = New System.Drawing.Size(72, 23) Me.lblAfter.TabIndex = 19 ' 'Label5 ' Me.Label5.Location = New System.Drawing.Point(296, 16) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(32, 23) Me.Label5.TabIndex = 20 Me.Label5.Text = "=?" ' 'Demo ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(488, 266) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.lblAfter) Me.Controls.Add(Me.lblBefore) Me.Controls.Add(Me.btnResetUserInput) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.optMale) Me.Controls.Add(Me.optFemale) Me.Controls.Add(Me.ListBox1) Me.Controls.Add(Me.btnRecall) Me.Controls.Add(Me.btnSave) Me.Controls.Add(Me.cbState) Me.Controls.Add(Me.dtpDOB) Me.Controls.Add(Me.txtDoNotSave) Me.Controls.Add(Me.txtName) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Name = "Demo" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim arPepBoysBefore As New ArrayList Dim arPepBoysAfter As New ArrayList arPepBoysBefore.Add("Manny") arPepBoysBefore.Add("Moe") arPepBoysBefore.Add("Jack") lblBefore.Text = arPepBoysBefore(2) Dim fs As New FileStream("c:\PepBoys.txt", FileMode.Create, FileAccess.Write) Dim BinFormatter As New Binary.BinaryFormatter BinFormatter.Serialize(fs, arPepBoysBefore) fs.Close() 'read back the ArrayList Dim fs1 As New FileStream("c:\PepBoys.txt", FileMode.Open, FileAccess.Read) arPepBoysAfter = BinFormatter.Deserialize(fs1) fs1.Close() lblAfter.Text = arPepBoysAfter(2) End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim htControlState As New Hashtable 'For Each ctrl As Control In Me.Controls Dim ctrl As Control For Each ctrl In Me.Controls If ctrl.Tag = "SAVE" Then ctrl.BackColor = Color.Pink Dim tn As String = ctrl.Name ' Dim str As String Select Case True Case TypeOf ctrl Is RadioButton Dim rb As RadioButton = CType(ctrl, RadioButton) htControlState(tn) = rb.Checked Case TypeOf ctrl Is TextBox Dim tb As TextBox = CType(ctrl, TextBox) htControlState(tn) = tb.Text Case TypeOf ctrl Is ComboBox Dim cb As ComboBox = CType(ctrl, ComboBox) htControlState(tn) = cb.Text Case TypeOf ctrl Is DateTimePicker Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker) htControlState(tn) = dtp.Value End Select End If Next Dim fs As New FileStream("c:\ControlStateSnapshot.txt", FileMode.Create, FileAccess.Write) Dim BinFormatter As New Binary.BinaryFormatter BinFormatter.Serialize(fs, htControlState) fs.Close() End Sub Private Sub btnRecall_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecall.Click Dim htControlState As New Hashtable Dim BinFormatter As New Binary.BinaryFormatter Dim fs1 As New FileStream("c:\ControlStateSnapshot.txt", FileMode.Open, FileAccess.Read) ' htControlState = CType(BinFormatter.Deserialize(fs1), Hashtable) htControlState = BinFormatter.Deserialize(fs1) fs1.Close() ListBox1.Items.Clear() For Each var As DictionaryEntry In htControlState Dim str As String Str = var.Key.ToString & " = [" Str &= var.Value Str &= "]" ListBox1.Items.Add(Str) Next Dim ctrl As Control For Each ctrl In Me.Controls If ctrl.Tag = "SAVE" Then ctrl.BackColor = Color.LightGreen Dim tn As String = ctrl.Name Select Case True Case TypeOf ctrl Is RadioButton Dim rb As RadioButton = CType(ctrl, RadioButton) rb.Checked = htControlState(tn) Case TypeOf ctrl Is TextBox Dim tb As TextBox = CType(ctrl, TextBox) tb.Text = htControlState(tn) Case TypeOf ctrl Is ComboBox Dim cb As ComboBox = CType(ctrl, ComboBox) cb.Text = htControlState(tn) Case TypeOf ctrl Is DateTimePicker Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker) dtp.Value = htControlState(tn) End Select End If Next End Sub Private Sub btnResetUserInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetUserInput.Click txtDoNotSave.Text = "" txtName.Text = "" cbState.Text = "State" txtDoNotSave.Text = "" ListBox1.Items.Clear() dtpDOB.Value = #1/1/2001# For Each ctrl As Control In Me.Controls If ctrl.Tag = "SAVE" Then ctrl.BackColor = Color.White Next End Sub End Class