Public Class frmRandom
  Inherits System.Windows.Forms.Form

  ' ====================== Members ====================
  Private MyList As New clsList()

  Private mFileName As String
  Private CurrentRecord As Long
  Private LastRecord As Long

#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 Panel1 As System.Windows.Forms.Panel
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents txtFirst As System.Windows.Forms.TextBox
  Friend WithEvents txtLast As System.Windows.Forms.TextBox
  Friend WithEvents txtCity As System.Windows.Forms.TextBox
  Friend WithEvents txtAddr As System.Windows.Forms.TextBox
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents Label5 As System.Windows.Forms.Label
  Friend WithEvents txtState As System.Windows.Forms.TextBox
  Friend WithEvents Label6 As System.Windows.Forms.Label
  Friend WithEvents btnAddNew As System.Windows.Forms.Button
  Friend WithEvents btnExit As System.Windows.Forms.Button
  Friend WithEvents txtZip As System.Windows.Forms.TextBox
  Friend WithEvents btnRead As System.Windows.Forms.Button
  Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
  Friend WithEvents btnFirst As System.Windows.Forms.Button
  Friend WithEvents btnnext As System.Windows.Forms.Button
  Friend WithEvents btnPrevious As System.Windows.Forms.Button
  Friend WithEvents btnLast As System.Windows.Forms.Button
  Friend WithEvents btnSave As System.Windows.Forms.Button
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Panel1 = New System.Windows.Forms.Panel()
    Me.txtZip = New System.Windows.Forms.TextBox()
    Me.Label6 = New System.Windows.Forms.Label()
    Me.txtState = New System.Windows.Forms.TextBox()
    Me.Label5 = New System.Windows.Forms.Label()
    Me.txtCity = New System.Windows.Forms.TextBox()
    Me.txtAddr = New System.Windows.Forms.TextBox()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.txtLast = New System.Windows.Forms.TextBox()
    Me.txtFirst = New System.Windows.Forms.TextBox()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.btnAddNew = New System.Windows.Forms.Button()
    Me.btnExit = New System.Windows.Forms.Button()
    Me.btnRead = New System.Windows.Forms.Button()
    Me.GroupBox1 = New System.Windows.Forms.GroupBox()
    Me.btnFirst = New System.Windows.Forms.Button()
    Me.btnnext = New System.Windows.Forms.Button()
    Me.btnPrevious = New System.Windows.Forms.Button()
    Me.btnLast = New System.Windows.Forms.Button()
    Me.btnSave = New System.Windows.Forms.Button()
    Me.Panel1.SuspendLayout()
    Me.GroupBox1.SuspendLayout()
    Me.SuspendLayout()
    '
    'Panel1
    '
    Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Panel1.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtZip, Me.Label6, Me.txtState, Me.Label5, Me.txtCity, Me.txtAddr, Me.Label3, Me.Label4, Me.txtLast, Me.txtFirst, Me.Label2, Me.Label1})
    Me.Panel1.Location = New System.Drawing.Point(8, 8)
    Me.Panel1.Name = "Panel1"
    Me.Panel1.Size = New System.Drawing.Size(368, 88)
    Me.Panel1.TabIndex = 0
    '
    'txtZip
    '
    Me.txtZip.Location = New System.Drawing.Point(304, 56)
    Me.txtZip.Name = "txtZip"
    Me.txtZip.Size = New System.Drawing.Size(56, 20)
    Me.txtZip.TabIndex = 11
    Me.txtZip.Text = ""
    '
    'Label6
    '
    Me.Label6.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label6.Location = New System.Drawing.Point(264, 56)
    Me.Label6.Name = "Label6"
    Me.Label6.Size = New System.Drawing.Size(40, 20)
    Me.Label6.TabIndex = 10
    Me.Label6.Text = "Zip:"
    Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'txtState
    '
    Me.txtState.Location = New System.Drawing.Point(232, 56)
    Me.txtState.Name = "txtState"
    Me.txtState.Size = New System.Drawing.Size(32, 20)
    Me.txtState.TabIndex = 9
    Me.txtState.Text = ""
    '
    'Label5
    '
    Me.Label5.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label5.Location = New System.Drawing.Point(192, 56)
    Me.Label5.Name = "Label5"
    Me.Label5.Size = New System.Drawing.Size(40, 20)
    Me.Label5.TabIndex = 8
    Me.Label5.Text = "State:"
    Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'txtCity
    '
    Me.txtCity.Location = New System.Drawing.Point(56, 56)
    Me.txtCity.Name = "txtCity"
    Me.txtCity.Size = New System.Drawing.Size(136, 20)
    Me.txtCity.TabIndex = 7
    Me.txtCity.Text = ""
    '
    'txtAddr
    '
    Me.txtAddr.Location = New System.Drawing.Point(56, 32)
    Me.txtAddr.Name = "txtAddr"
    Me.txtAddr.Size = New System.Drawing.Size(304, 20)
    Me.txtAddr.TabIndex = 6
    Me.txtAddr.Text = ""
    '
    'Label3
    '
    Me.Label3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label3.Location = New System.Drawing.Point(8, 56)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(48, 20)
    Me.Label3.TabIndex = 5
    Me.Label3.Text = "City:"
    Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'Label4
    '
    Me.Label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label4.Location = New System.Drawing.Point(8, 32)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(48, 20)
    Me.Label4.TabIndex = 4
    Me.Label4.Text = "Addr:"
    Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'txtLast
    '
    Me.txtLast.Location = New System.Drawing.Point(200, 8)
    Me.txtLast.Name = "txtLast"
    Me.txtLast.Size = New System.Drawing.Size(160, 20)
    Me.txtLast.TabIndex = 3
    Me.txtLast.Text = ""
    '
    'txtFirst
    '
    Me.txtFirst.Location = New System.Drawing.Point(56, 8)
    Me.txtFirst.Name = "txtFirst"
    Me.txtFirst.Size = New System.Drawing.Size(104, 20)
    Me.txtFirst.TabIndex = 2
    Me.txtFirst.Text = ""
    '
    'Label2
    '
    Me.Label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label2.Location = New System.Drawing.Point(160, 8)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(40, 20)
    Me.Label2.TabIndex = 1
    Me.Label2.Text = "Last:"
    Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'Label1
    '
    Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.Label1.Location = New System.Drawing.Point(8, 8)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(48, 20)
    Me.Label1.TabIndex = 0
    Me.Label1.Text = "First:"
    Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
    '
    'btnAddNew
    '
    Me.btnAddNew.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnAddNew.Location = New System.Drawing.Point(8, 104)
    Me.btnAddNew.Name = "btnAddNew"
    Me.btnAddNew.Size = New System.Drawing.Size(72, 24)
    Me.btnAddNew.TabIndex = 1
    Me.btnAddNew.Text = "&New"
    '
    'btnExit
    '
    Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnExit.Location = New System.Drawing.Point(288, 104)
    Me.btnExit.Name = "btnExit"
    Me.btnExit.Size = New System.Drawing.Size(88, 24)
    Me.btnExit.TabIndex = 2
    Me.btnExit.Text = "E&xit"
    '
    'btnRead
    '
    Me.btnRead.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnRead.Location = New System.Drawing.Point(88, 104)
    Me.btnRead.Name = "btnRead"
    Me.btnRead.Size = New System.Drawing.Size(72, 24)
    Me.btnRead.TabIndex = 3
    Me.btnRead.Text = "&Read"
    '
    'GroupBox1
    '
    Me.GroupBox1.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnLast, Me.btnPrevious, Me.btnnext, Me.btnFirst})
    Me.GroupBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.GroupBox1.Location = New System.Drawing.Point(8, 144)
    Me.GroupBox1.Name = "GroupBox1"
    Me.GroupBox1.Size = New System.Drawing.Size(368, 48)
    Me.GroupBox1.TabIndex = 4
    Me.GroupBox1.TabStop = False
    Me.GroupBox1.Text = "Read Record :"
    '
    'btnFirst
    '
    Me.btnFirst.Location = New System.Drawing.Point(16, 16)
    Me.btnFirst.Name = "btnFirst"
    Me.btnFirst.Size = New System.Drawing.Size(72, 20)
    Me.btnFirst.TabIndex = 0
    Me.btnFirst.Text = "&First"
    '
    'btnnext
    '
    Me.btnnext.Location = New System.Drawing.Point(104, 16)
    Me.btnnext.Name = "btnnext"
    Me.btnnext.Size = New System.Drawing.Size(72, 20)
    Me.btnnext.TabIndex = 1
    Me.btnnext.Text = "&Next"
    '
    'btnPrevious
    '
    Me.btnPrevious.Location = New System.Drawing.Point(192, 16)
    Me.btnPrevious.Name = "btnPrevious"
    Me.btnPrevious.Size = New System.Drawing.Size(72, 20)
    Me.btnPrevious.TabIndex = 2
    Me.btnPrevious.Text = "&Previous"
    '
    'btnLast
    '
    Me.btnLast.Location = New System.Drawing.Point(280, 16)
    Me.btnLast.Name = "btnLast"
    Me.btnLast.Size = New System.Drawing.Size(72, 20)
    Me.btnLast.TabIndex = 3
    Me.btnLast.Text = "&Last"
    '
    'btnSave
    '
    Me.btnSave.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.btnSave.Location = New System.Drawing.Point(168, 104)
    Me.btnSave.Name = "btnSave"
    Me.btnSave.Size = New System.Drawing.Size(72, 24)
    Me.btnSave.TabIndex = 5
    Me.btnSave.Text = "&Save"
    '
    'frmRandom
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(384, 205)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnSave, Me.GroupBox1, Me.btnRead, Me.btnExit, Me.btnAddNew, Me.Panel1})
    Me.Name = "frmRandom"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Random Access Files"
    Me.Panel1.ResumeLayout(False)
    Me.GroupBox1.ResumeLayout(False)
    Me.ResumeLayout(False)

  End Sub

#End Region

  ' ====================== Properties =================
  Public Property FileName() As String
    Get
      Return mFileName
    End Get

    Set(ByVal NewFileName As String)
      mFileName = NewFileName
    End Set

  End Property

  ' ================= Form Load Event ====================
  Private Sub frmRandom_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim i As Integer

    MyList.FileName = mFileName             ' Copy open file name to class
    LastRecord = MyList.CurrentRecordCount  ' How many records there?
    CurrentRecord = 1
    If LastRecord > 0 Then                  ' If there are any records...
      FillARecord(1)                        ' ...show the first one
    End If
  End Sub

  Private Sub txtState_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtState.Leave
    txtState.Text = UCase(txtState.Text)    ' Just convert state to upper case
  End Sub

  Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
    Dim NewRecord As Long
    ClearForm()
    txtFirst.Focus()
  End Sub

  Private Sub ClearForm()     ' Just clear the text boxes
    txtFirst.Text = ""
    txtLast.Text = ""
    txtAddr.Text = ""
    txtCity.Text = ""
    txtState.Text = ""
    txtZip.Text = ""
  End Sub

  Private Sub FillARecord(ByVal rec As Long)
    ' Purpose:  This subroutine is used to fill in the form's
    '           text boxes.
    '
    ' Argument list:
    '   rec           a long that is the record number to retrieve
    '
    Dim ErrorFlag As Integer

    ErrorFlag = MyList.ReadRecord(rec)  ' Read the class record
    If ErrorFlag = 0 Then
      Me.Text = "Random Access Files    Record: " & CStr(rec) ' Set the title
      txtFirst.Text = MyList.First
      txtLast.Text = MyList.Last
      txtAddr.Text = MyList.Addr
      txtCity.Text = MyList.City
      txtState.Text = MyList.State
      txtZip.Text = MyList.Zip
    Else
      MessageBox.Show("Read failure")
    End If
  End Sub

  Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
    Dim response As String

    response = InputBox("Enter the record number:", "Valid Record Numbers 1 through " & CStr(MyList.CurrentRecordCount))
    FillARecord(CLng(response))
  End Sub

  ' ===================== Navigation buttons ===========================
  Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
    CurrentRecord = 1
    FillARecord(CurrentRecord)
  End Sub

  Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
    CurrentRecord += 1
    If CurrentRecord > LastRecord Then
      CurrentRecord = LastRecord
    End If
    FillARecord(CurrentRecord)
  End Sub

  Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
    CurrentRecord = LastRecord
    If CurrentRecord < 1 Then
      CurrentRecord = 1
    End If
    FillARecord(CurrentRecord)
  End Sub

  Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
    CurrentRecord -= 1
    If CurrentRecord < 1 Then
      CurrentRecord = 1
    End If
    FillARecord(CurrentRecord)

  End Sub


  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    Dim NewRecord As Integer, ErrorFlag As Integer

    MyList.First = txtFirst.Text    ' Copy the data to class members
    MyList.Last = txtLast.Text
    MyList.Addr = txtAddr.Text
    MyList.City = txtCity.Text
    MyList.State = txtState.Text
    MyList.Zip = txtZip.Text

    NewRecord = MyList.CurrentRecordCount + 1 ' Bump the record counter
    MyList.WriteRecord(NewRecord)
    LastRecord = NewRecord
    If ErrorFlag = 0 Then
      MessageBox.Show("Data written to file.")
    End If

  End Sub

  Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    Me.Dispose()
  End Sub

End Class
