Public Class Form1
    Inherits System.Windows.Forms.Form

    ' members...
    Private _customers As New CustomerCollection()

#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 txtFirstName As System.Windows.Forms.TextBox
    Friend WithEvents txtLastName As System.Windows.Forms.TextBox
    Friend WithEvents txtEmail As System.Windows.Forms.TextBox
    Friend WithEvents txtName As System.Windows.Forms.TextBox
    Friend WithEvents btnTest As System.Windows.Forms.Button
    Friend WithEvents lstCustomers As System.Windows.Forms.ListBox
    Friend WithEvents lblEmail As System.Windows.Forms.Label
    Friend WithEvents lblName As System.Windows.Forms.Label
    Friend WithEvents lblFirstName As System.Windows.Forms.Label
    Friend WithEvents lblLastName As System.Windows.Forms.Label
    Friend WithEvents btnDelete 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.txtEmail = New System.Windows.Forms.TextBox()
        Me.btnDelete = New System.Windows.Forms.Button()
        Me.lblLastName = New System.Windows.Forms.Label()
        Me.btnTest = New System.Windows.Forms.Button()
        Me.txtName = New System.Windows.Forms.TextBox()
        Me.txtLastName = New System.Windows.Forms.TextBox()
        Me.lblName = New System.Windows.Forms.Label()
        Me.lblFirstName = New System.Windows.Forms.Label()
        Me.txtFirstName = New System.Windows.Forms.TextBox()
        Me.lblEmail = New System.Windows.Forms.Label()
        Me.lstCustomers = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'txtEmail
        '
        Me.txtEmail.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.txtEmail.Location = New System.Drawing.Point(76, 164)
        Me.txtEmail.Name = "txtEmail"
        Me.txtEmail.Size = New System.Drawing.Size(252, 20)
        Me.txtEmail.TabIndex = 0
        Me.txtEmail.Text = ""
        '
        'btnDelete
        '
        Me.btnDelete.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.btnDelete.Location = New System.Drawing.Point(184, 192)
        Me.btnDelete.Name = "btnDelete"
        Me.btnDelete.Size = New System.Drawing.Size(80, 24)
        Me.btnDelete.TabIndex = 2
        Me.btnDelete.Text = "Delete"
        '
        'lblLastName
        '
        Me.lblLastName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.lblLastName.Location = New System.Drawing.Point(8, 144)
        Me.lblLastName.Name = "lblLastName"
        Me.lblLastName.Size = New System.Drawing.Size(64, 16)
        Me.lblLastName.TabIndex = 1
        Me.lblLastName.Text = "Last Name:"
        '
        'btnTest
        '
        Me.btnTest.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.btnTest.Location = New System.Drawing.Point(96, 192)
        Me.btnTest.Name = "btnTest"
        Me.btnTest.Size = New System.Drawing.Size(80, 24)
        Me.btnTest.TabIndex = 2
        Me.btnTest.Text = "Test"
        '
        'txtName
        '
        Me.txtName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.txtName.Location = New System.Drawing.Point(76, 92)
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(252, 20)
        Me.txtName.TabIndex = 0
        Me.txtName.Text = ""
        '
        'txtLastName
        '
        Me.txtLastName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.txtLastName.Location = New System.Drawing.Point(76, 140)
        Me.txtLastName.Name = "txtLastName"
        Me.txtLastName.Size = New System.Drawing.Size(252, 20)
        Me.txtLastName.TabIndex = 0
        Me.txtLastName.Text = ""
        '
        'lblName
        '
        Me.lblName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.lblName.Location = New System.Drawing.Point(8, 96)
        Me.lblName.Name = "lblName"
        Me.lblName.Size = New System.Drawing.Size(60, 16)
        Me.lblName.TabIndex = 1
        Me.lblName.Text = "Name:"
        '
        'lblFirstName
        '
        Me.lblFirstName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.lblFirstName.Location = New System.Drawing.Point(8, 120)
        Me.lblFirstName.Name = "lblFirstName"
        Me.lblFirstName.Size = New System.Drawing.Size(64, 16)
        Me.lblFirstName.TabIndex = 1
        Me.lblFirstName.Text = "First Name:"
        '
        'txtFirstName
        '
        Me.txtFirstName.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.txtFirstName.Location = New System.Drawing.Point(76, 116)
        Me.txtFirstName.Name = "txtFirstName"
        Me.txtFirstName.Size = New System.Drawing.Size(252, 20)
        Me.txtFirstName.TabIndex = 0
        Me.txtFirstName.Text = ""
        '
        'lblEmail
        '
        Me.lblEmail.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left)
        Me.lblEmail.Location = New System.Drawing.Point(8, 168)
        Me.lblEmail.Name = "lblEmail"
        Me.lblEmail.Size = New System.Drawing.Size(64, 16)
        Me.lblEmail.TabIndex = 1
        Me.lblEmail.Text = "E-mail:"
        '
        'lstCustomers
        '
        Me.lstCustomers.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right)
        Me.lstCustomers.IntegralHeight = False
        Me.lstCustomers.Location = New System.Drawing.Point(8, 8)
        Me.lstCustomers.Name = "lstCustomers"
        Me.lstCustomers.Size = New System.Drawing.Size(320, 76)
        Me.lstCustomers.TabIndex = 3
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(336, 221)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnDelete, Me.lstCustomers, Me.btnTest, Me.lblName, Me.lblFirstName, Me.txtFirstName, Me.lblEmail, Me.lblLastName, Me.txtLastName, Me.txtEmail, Me.txtName})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region



    Private Sub btnTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTest.Click

        ' create some customers...
        CreateCustomer("Amir", "Aliabadi", "amir@pretendcompany.com")
        CreateCustomer("Gretchen", "Aliabadi", _
                       "gretchen@pretendcompany.com")
        CreateCustomer("Ben", "Aliabadi", "ben@pretendcompany.com")


    End Sub

    ' DisplayCustomer - show the customer...
    Public Sub DisplayCustomer(ByVal customer As Customer)

        ' update the fields...
        txtName.Text = customer.Name
        txtFirstName.Text = customer.FirstName
        txtLastName.Text = customer.LastName
        txtEmail.Text = customer.Email

    End Sub


    ' CreateCustomer - create a new customer...
    Public Function CreateCustomer(ByVal firstName As String, _
         ByVal lastName As String, ByVal email As String) As Customer

        ' create the new customer...
        Dim newCustomer As Customer
        newCustomer.FirstName = firstName
        newCustomer.LastName = lastName
        newCustomer.Email = email

        ' add it to the list...
        _customers.Add(newCustomer)

        ' add it to the view...
        lstCustomers.Items.Add(newCustomer)

        ' return the customer...
        Return newCustomer

    End Function


    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        ' what customer to we want to select?
        If SelectedCustomer.IsEmpty = False Then

            ' ask the user...
            If MessageBox.Show("Are you sure you want to delete " & _
               SelectedCustomer.Name & "?", "Structure Demo", _
               MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
               = DialogResult.Yes Then

                ' store the customer that we want to delete...
                Dim deleteCustomer As Customer = SelectedCustomer

                ' remove it from the arraylist...
                _customers.Remove(deleteCustomer)

                ' remove it from the list view...
                lstCustomers.Items.Remove(deleteCustomer)

            End If

        Else
            MessageBox.Show("You must select a customer.", _
                            "Structure Demo")
        End If

    End Sub


    Public ReadOnly Property SelectedCustomer() As Customer
        Get

            ' do we have a selection?
            If lstCustomers.SelectedIndex <> -1 Then

                ' return the selected customer...
                Return _customers(lstCustomers.SelectedIndex)

            End If

        End Get
    End Property


    Private Sub lstCustomers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstCustomers.SelectedIndexChanged
        DisplayCustomer(SelectedCustomer)
    End Sub
End Class
