Public Class Form1
    Inherits System.Windows.Forms.Form

    ' members...
    Public AddressBook As AddressBook
    Private _currentAddressIndex As Integer


    ' CurrentAddress - property for the current address...
    ReadOnly Property CurrentAddress() As Address
        Get
            Return AddressBook.Items(CurrentAddressIndex - 1)
        End Get
    End Property


    ' CurrentAddressIndex - property for the current address...
    Property CurrentAddressIndex() As Integer
        Get
            Return _currentAddressIndex
        End Get
        Set(ByVal Value As Integer)

            ' set the address...
            _currentAddressIndex = Value

            ' update the display...
            PopulateFormFromAddress(CurrentAddress)

            ' set the label...
            lblAddressNumber.Text = _
            _currentAddressIndex & " of " & AddressBook.Items.Count

        End Set
    End Property


#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 btnDelete As System.Windows.Forms.Button
    Friend WithEvents btnNew As System.Windows.Forms.Button
    Friend WithEvents btnNext As System.Windows.Forms.Button
    Friend WithEvents btnPrevious As System.Windows.Forms.Button
    Friend WithEvents linkSendEmail As System.Windows.Forms.LinkLabel
    Friend WithEvents txtEmail As System.Windows.Forms.TextBox
    Friend WithEvents txtCountry As System.Windows.Forms.TextBox
    Friend WithEvents txtPostalCode As System.Windows.Forms.TextBox
    Friend WithEvents txtRegion As System.Windows.Forms.TextBox
    Friend WithEvents txtCity As System.Windows.Forms.TextBox
    Friend WithEvents txtAddress2 As System.Windows.Forms.TextBox
    Friend WithEvents txtAddress1 As System.Windows.Forms.TextBox
    Friend WithEvents txtCompanyName As System.Windows.Forms.TextBox
    Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
    Friend WithEvents lblAddressNumber As System.Windows.Forms.Label
    Friend WithEvents Label11 As System.Windows.Forms.Label
    Friend WithEvents Label10 As System.Windows.Forms.Label
    Friend WithEvents Label9 As System.Windows.Forms.Label
    Friend WithEvents Label8 As System.Windows.Forms.Label
    Friend WithEvents Label7 As System.Windows.Forms.Label
    Friend WithEvents Label6 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents Label4 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnDelete = New System.Windows.Forms.Button()
        Me.btnNew = New System.Windows.Forms.Button()
        Me.btnNext = New System.Windows.Forms.Button()
        Me.btnPrevious = New System.Windows.Forms.Button()
        Me.linkSendEmail = New System.Windows.Forms.LinkLabel()
        Me.txtEmail = New System.Windows.Forms.TextBox()
        Me.txtCountry = New System.Windows.Forms.TextBox()
        Me.txtPostalCode = New System.Windows.Forms.TextBox()
        Me.txtRegion = New System.Windows.Forms.TextBox()
        Me.txtCity = New System.Windows.Forms.TextBox()
        Me.txtAddress2 = New System.Windows.Forms.TextBox()
        Me.txtAddress1 = New System.Windows.Forms.TextBox()
        Me.txtCompanyName = New System.Windows.Forms.TextBox()
        Me.txtLastName = New System.Windows.Forms.TextBox()
        Me.txtFirstName = New System.Windows.Forms.TextBox()
        Me.lblAddressNumber = New System.Windows.Forms.Label()
        Me.Label11 = New System.Windows.Forms.Label()
        Me.Label10 = New System.Windows.Forms.Label()
        Me.Label9 = New System.Windows.Forms.Label()
        Me.Label8 = New System.Windows.Forms.Label()
        Me.Label7 = New System.Windows.Forms.Label()
        Me.Label6 = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Label4 = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'btnDelete
        '
        Me.btnDelete.Location = New System.Drawing.Point(368, 376)
        Me.btnDelete.Name = "btnDelete"
        Me.btnDelete.Size = New System.Drawing.Size(104, 32)
        Me.btnDelete.TabIndex = 30
        Me.btnDelete.Text = "Delete"
        '
        'btnNew
        '
        Me.btnNew.Location = New System.Drawing.Point(256, 376)
        Me.btnNew.Name = "btnNew"
        Me.btnNew.Size = New System.Drawing.Size(104, 32)
        Me.btnNew.TabIndex = 29
        Me.btnNew.Text = "New"
        '
        'btnNext
        '
        Me.btnNext.Location = New System.Drawing.Point(136, 376)
        Me.btnNext.Name = "btnNext"
        Me.btnNext.Size = New System.Drawing.Size(104, 32)
        Me.btnNext.TabIndex = 32
        Me.btnNext.Text = "Next"
        '
        'btnPrevious
        '
        Me.btnPrevious.Location = New System.Drawing.Point(16, 376)
        Me.btnPrevious.Name = "btnPrevious"
        Me.btnPrevious.Size = New System.Drawing.Size(104, 32)
        Me.btnPrevious.TabIndex = 31
        Me.btnPrevious.Text = "Previous"
        '
        'linkSendEmail
        '
        Me.linkSendEmail.Location = New System.Drawing.Point(368, 328)
        Me.linkSendEmail.Name = "linkSendEmail"
        Me.linkSendEmail.Size = New System.Drawing.Size(96, 24)
        Me.linkSendEmail.TabIndex = 28
        Me.linkSendEmail.TabStop = True
        Me.linkSendEmail.Text = "Send E-mail"
        '
        'txtEmail
        '
        Me.txtEmail.Location = New System.Drawing.Point(160, 328)
        Me.txtEmail.Name = "txtEmail"
        Me.txtEmail.Size = New System.Drawing.Size(184, 20)
        Me.txtEmail.TabIndex = 22
        Me.txtEmail.Text = ""
        '
        'txtCountry
        '
        Me.txtCountry.Location = New System.Drawing.Point(160, 296)
        Me.txtCountry.Name = "txtCountry"
        Me.txtCountry.Size = New System.Drawing.Size(184, 20)
        Me.txtCountry.TabIndex = 23
        Me.txtCountry.Text = ""
        '
        'txtPostalCode
        '
        Me.txtPostalCode.Location = New System.Drawing.Point(160, 264)
        Me.txtPostalCode.Name = "txtPostalCode"
        Me.txtPostalCode.Size = New System.Drawing.Size(160, 20)
        Me.txtPostalCode.TabIndex = 20
        Me.txtPostalCode.Text = ""
        '
        'txtRegion
        '
        Me.txtRegion.Location = New System.Drawing.Point(160, 232)
        Me.txtRegion.Name = "txtRegion"
        Me.txtRegion.Size = New System.Drawing.Size(184, 20)
        Me.txtRegion.TabIndex = 21
        Me.txtRegion.Text = ""
        '
        'txtCity
        '
        Me.txtCity.Location = New System.Drawing.Point(160, 200)
        Me.txtCity.Name = "txtCity"
        Me.txtCity.Size = New System.Drawing.Size(184, 20)
        Me.txtCity.TabIndex = 26
        Me.txtCity.Text = ""
        '
        'txtAddress2
        '
        Me.txtAddress2.Location = New System.Drawing.Point(160, 168)
        Me.txtAddress2.Name = "txtAddress2"
        Me.txtAddress2.Size = New System.Drawing.Size(304, 20)
        Me.txtAddress2.TabIndex = 27
        Me.txtAddress2.Text = ""
        '
        'txtAddress1
        '
        Me.txtAddress1.Location = New System.Drawing.Point(160, 136)
        Me.txtAddress1.Name = "txtAddress1"
        Me.txtAddress1.Size = New System.Drawing.Size(304, 20)
        Me.txtAddress1.TabIndex = 24
        Me.txtAddress1.Text = ""
        '
        'txtCompanyName
        '
        Me.txtCompanyName.Location = New System.Drawing.Point(160, 104)
        Me.txtCompanyName.Name = "txtCompanyName"
        Me.txtCompanyName.Size = New System.Drawing.Size(304, 20)
        Me.txtCompanyName.TabIndex = 25
        Me.txtCompanyName.Text = ""
        '
        'txtLastName
        '
        Me.txtLastName.Location = New System.Drawing.Point(160, 72)
        Me.txtLastName.Name = "txtLastName"
        Me.txtLastName.Size = New System.Drawing.Size(304, 20)
        Me.txtLastName.TabIndex = 19
        Me.txtLastName.Text = ""
        '
        'txtFirstName
        '
        Me.txtFirstName.Location = New System.Drawing.Point(160, 40)
        Me.txtFirstName.Name = "txtFirstName"
        Me.txtFirstName.Size = New System.Drawing.Size(304, 20)
        Me.txtFirstName.TabIndex = 18
        Me.txtFirstName.Text = ""
        '
        'lblAddressNumber
        '
        Me.lblAddressNumber.Location = New System.Drawing.Point(160, 16)
        Me.lblAddressNumber.Name = "lblAddressNumber"
        Me.lblAddressNumber.Size = New System.Drawing.Size(272, 16)
        Me.lblAddressNumber.TabIndex = 10
        Me.lblAddressNumber.Text = "(number)"
        '
        'Label11
        '
        Me.Label11.Location = New System.Drawing.Point(16, 336)
        Me.Label11.Name = "Label11"
        Me.Label11.Size = New System.Drawing.Size(128, 16)
        Me.Label11.TabIndex = 11
        Me.Label11.Text = "E-mail:"
        '
        'Label10
        '
        Me.Label10.Location = New System.Drawing.Point(16, 304)
        Me.Label10.Name = "Label10"
        Me.Label10.Size = New System.Drawing.Size(128, 16)
        Me.Label10.TabIndex = 9
        Me.Label10.Text = "Country:"
        '
        'Label9
        '
        Me.Label9.Location = New System.Drawing.Point(16, 272)
        Me.Label9.Name = "Label9"
        Me.Label9.Size = New System.Drawing.Size(128, 16)
        Me.Label9.TabIndex = 7
        Me.Label9.Text = "Postal Code:"
        '
        'Label8
        '
        Me.Label8.Location = New System.Drawing.Point(16, 240)
        Me.Label8.Name = "Label8"
        Me.Label8.Size = New System.Drawing.Size(128, 16)
        Me.Label8.TabIndex = 8
        Me.Label8.Text = "Region:"
        '
        'Label7
        '
        Me.Label7.Location = New System.Drawing.Point(16, 208)
        Me.Label7.Name = "Label7"
        Me.Label7.Size = New System.Drawing.Size(128, 16)
        Me.Label7.TabIndex = 12
        Me.Label7.Text = "City:"
        '
        'Label6
        '
        Me.Label6.Location = New System.Drawing.Point(16, 176)
        Me.Label6.Name = "Label6"
        Me.Label6.Size = New System.Drawing.Size(128, 16)
        Me.Label6.TabIndex = 16
        Me.Label6.Text = "Address 2:"
        '
        'Label5
        '
        Me.Label5.Location = New System.Drawing.Point(16, 144)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(128, 16)
        Me.Label5.TabIndex = 17
        Me.Label5.Text = "Address 1:"
        '
        'Label4
        '
        Me.Label4.Location = New System.Drawing.Point(16, 112)
        Me.Label4.Name = "Label4"
        Me.Label4.Size = New System.Drawing.Size(128, 16)
        Me.Label4.TabIndex = 15
        Me.Label4.Text = "Company Name:"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(16, 80)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(128, 16)
        Me.Label3.TabIndex = 13
        Me.Label3.Text = "Last Name:"
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(16, 48)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(128, 16)
        Me.Label2.TabIndex = 14
        Me.Label2.Text = "First Name:"
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(128, 16)
        Me.Label1.TabIndex = 6
        Me.Label1.Text = "Contact Number:"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 421)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnDelete, Me.btnNew, Me.btnNext, Me.btnPrevious, Me.linkSendEmail, Me.txtEmail, Me.txtCountry, Me.txtPostalCode, Me.txtRegion, Me.txtCity, Me.txtAddress2, Me.txtAddress1, Me.txtCompanyName, Me.txtLastName, Me.txtFirstName, Me.lblAddressNumber, Me.Label11, Me.Label10, Me.Label9, Me.Label8, Me.Label7, Me.Label6, Me.Label5, Me.Label4, Me.Label3, Me.Label2, Me.Label1})
        Me.Name = "Form1"
        Me.Text = "Address Book"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' create a new address object...
        Dim address As New Address()

        ' copy the values from the form into the address...
        PopulateAddressFromForm(address)

        ' save the address...
        Dim filename As String = DataFilename
        address.Save(filename)

        ' tell the user...
        MsgBox("The address was saved to " & filename)

    End Sub


    ' DataFilename - where should we store our data?
    Public ReadOnly Property DataFilename() As String
        Get

            ' get our working folder...
            Dim folder As String
            folder = Environment.CurrentDirectory

            ' return the folder with the name "addressbook.xml"...
            Return folder & "\AddressBook.xml"

        End Get
    End Property


    ' PopulateAddressFromForm - populates Address from the form fields...
    Public Sub PopulateAddressFromForm(ByVal address As Address)
        ' copy the values...
        address.FirstName = txtFirstName.Text
        address.LastName = txtLastName.Text
        address.CompanyName = txtCompanyName.Text
        address.Address1 = txtAddress1.Text
        address.Address2 = txtAddress2.Text
        address.City = txtCity.Text
        address.Region = txtRegion.Text
        address.PostalCode = txtPostalCode.Text
        address.Country = txtCountry.Text
        address.Email = txtEmail.Text

    End Sub


    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' load the address using a shared method on SerializableData...
        Dim newAddress As Address = _
         SerializableData.Load(DataFilename, GetType(Address))

        ' update the display...
        PopulateFormFromAddress(newAddress)
    End Sub


    ' PopulateFormFromAddress - populates the form from an 
    ' address object...
    Public Sub PopulateFormFromAddress(ByVal address As Address)

        ' copy the values...
        txtFirstName.Text = address.FirstName
        txtLastName.Text = address.LastName
        txtCompanyName.Text = address.CompanyName
        txtAddress1.Text = address.Address1
        txtAddress2.Text = address.Address2
        txtCity.Text = address.City
        txtRegion.Text = address.Region
        txtPostalCode.Text = address.PostalCode
        txtCountry.Text = address.Country
        txtEmail.Text = address.Email

    End Sub


    Private Sub linkSendEmail_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linkSendEmail.LinkClicked
        ' start the e-mail client...
        System.Diagnostics.Process.Start("mailto:" & txtEmail.Text)
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' load the address book...
        AddressBook = SerializableData.Load(DataFilename, GetType(AddressBook))

        ' if the address book only contains one item, add a new one...
        If AddressBook.Items.Count = 0 Then AddressBook.AddAddress()

        ' select the first item in the list...
        CurrentAddressIndex = 1
    End Sub


    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        ' save the changes...
        UpdateCurrentAddress()
        SaveChanges()
    End Sub


    ' SaveChanges - save the address book to an XML file...
    Public Sub SaveChanges()
        ' tell the address book to save itself...
        AddressBook.Save(DataFilename)
    End Sub


    ' UpdateCurrentAddress - make sure the book has the current
    ' values currently entered into the form...
    Private Sub UpdateCurrentAddress()
        PopulateAddressFromForm(CurrentAddress)
    End Sub


    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        AddNewAddress()
    End Sub


    Public Function AddNewAddress() As Address

        ' save the current address...
        UpdateCurrentAddress()

        ' create a new address...
        Dim newAddress As Address = AddressBook.AddAddress

        ' update the display...
        CurrentAddressIndex = AddressBook.Items.Count

        ' return the new address...
        Return newAddress

    End Function


    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        MoveNext()
    End Sub


    Public Sub MoveNext()

        ' get the next index...
        Dim newIndex As Integer = CurrentAddressIndex + 1
        If newIndex > AddressBook.Items.Count Then
            newIndex = 1
        End If

        ' save any changes...
        UpdateCurrentAddress()

        ' move the record...
        CurrentAddressIndex = newIndex
    End Sub



    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        MovePrevious()
    End Sub


    Public Sub MovePrevious()
        ' get the previous index...
        Dim newIndex As Integer = CurrentAddressIndex - 1
        If newIndex = 0 Then
            newIndex = AddressBook.Items.Count
        End If

        ' save changes...
        UpdateCurrentAddress()

        ' move the record...
        CurrentAddressIndex = newIndex
    End Sub


    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        ' ask the user if they are ok with this?
        If MsgBox("Are you sure you want to delete this address?", _
            MsgBoxStyle.Question Or MsgBoxStyle.YesNo) = _
            MsgBoxResult.Yes Then
            DeleteAddress(CurrentAddressIndex)
        End If
    End Sub


    ' DeleteAddress - delete an address from the list...
    Public Sub DeleteAddress(ByVal index As Integer)

        ' delete the item from the list...
        AddressBook.Items.RemoveAt(index - 1)

        ' was that the last address?
        If AddressBook.Items.Count = 0 Then
            ' add a new address?
            AddressBook.AddAddress()
        Else
            ' make sure we have something to show...
            If index > AddressBook.Items.Count Then
                index = AddressBook.Items.Count
            End If
        End If

        ' display the record...
        CurrentAddressIndex = index
    End Sub


End Class
