VB.Net: OOP Address Book


Overview​


This is a fully functional binary OOP Address Book which allows entry and retrieval of names, addresses (with shared address linking), birth dates, phone numbers, email addresses, photos, etc. All of this information is contained either in the Person class, or its component members.

Figure 1.0 Entering data

Figure 1.1 Saving data

Figure 1.2 Adding photos

Figure 1.3 Changing photo titles

↑ Back to top

The Address Book consists of a system core of objects coordinated by a coordinating class. The objects are, one composite class

  • Person (see figure 2.1)

All of the other objects are component members of the Person class, either as a single instance attribute, such as the Name class, or multiple instance objects, such as the Address and Photo classes:

  • Name (see figure 2.2)
  • Address (see figure 2.3)
  • Photo (see figure 2.4)

The Core Classes


Figure 2.0 Classes

Figure 2.1 The Person class

Figure 2.2 The Name class

Figure 2.3 The Address class

Figure 2.4 The Photo class

↑ Back to top


The GUI


The GUI is quite complex, consisting of six forms, all of which are dialog forms, with the exception of the main form, frmMain which remains open for the duration of program usage.

There are also five usercontrols, used mainly for data input, but these range from a simple extended panel with the only purpose of setting DoubleBuffered, to a simple UserControl hosted in a menustrip, used for editing a TabPage's Text, with the remaining three Controls used for data input.

It was challenging to keep the Form sizes to a minimum and also provide an intuitive user interface which provides means to enter and browse addresses and names, birthdays etc., making data entry as easy as possible by providing address suggestions based on a user entered search term. The Address Book is fully searchable, by:

  • FirstName Startswith
  • LastName Startswith
  • Business Name
  • Town
  • City
  • Region
  • PostCode
  • Landline

It also provides a search facility for searching for Person objects where the birthday falls within the current week.


The Details Control

↑ Back to top

Public Class  detailsControl
 
    Private Sub  ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal  e As  System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Select Case  ComboBox1.SelectedIndex
            Case 0
                ComboBox2.SelectedIndex = 0
            Case 1, 2, 3
                ComboBox2.SelectedIndex = 1
            Case Else
                ComboBox2.SelectedIndex = -1
        End Select
    End Sub
 
    Public Sub  setDetails(ByVal  p As  Person)
        ComboBox1.SelectedIndex = p.name.title
        TextBox1.Text = p.name.firstName
        TextBox2.Lines = p.name.middleName
        TextBox3.Text = p.name.lastName
        ComboBox2.SelectedIndex = p.Sex
        DateTimePicker1.Value = p.dob
    End Sub
 
    Public Function  getName() As  Name
        Dim m As New  List(Of String)(TextBox2.Lines)
        m.RemoveAll(Function(s) s.Trim = "")
        Return New  Name(DirectCast(ComboBox1.SelectedIndex, Name.personTitle), TextBox1.Text, m.ToArray, TextBox3.Text)
    End Function
 
    Public Function  getSex() As  Person.PersonSex
        Return DirectCast(ComboBox2.SelectedIndex, Person.PersonSex)
    End Function
 
    Public Function  getDOB() As  Date
        Return DateTimePicker1.Value.Date
    End Function
 
End Class

The addressControl

↑ Back to top

Public Class  addressControl
 
    Private use As usage
 
    Public Enum  usage
        Display
        Edit
    End Enum
 
    Private shareIndex As Integer  = 0
 
    Public Sub  New(ByVal use As usage, ByVal a As Address)
        InitializeComponent()
        If use = usage.Display Then
            PictureBox1.Visible = False
            Dim types() As Object  = New  Object() {Address.AType.PrimaryResidential, Address.AType.SecondaryResidential, Address.AType.PrimaryBusiness, Address.AType.SecondaryBusiness}
            ComboBox1.SelectedIndex = Array.IndexOf(types, a.addressType)
            ComboBox1.Enabled = False
            TextBox1.Text = a.addressLine1
            TextBox1.ReadOnly = True
            TextBox2.Text = a.addressLine2
            TextBox2.ReadOnly = True
            TextBox3.Text = a.town
            TextBox3.ReadOnly = True
            TextBox4.Text = a.city
            TextBox4.ReadOnly = True
            TextBox5.Text = a.region
            TextBox5.ReadOnly = True
            TextBox6.Text = a.postcode
            TextBox6.ReadOnly = True
            TextBox7.Text = a.country
            TextBox7.ReadOnly = True
            TextBox8.Text = a.landline
            TextBox8.ReadOnly = True
        Else
            If a IsNot Nothing Then
                Dim types() As Object  = New  Object() {Address.AType.PrimaryResidential, Address.AType.SecondaryResidential, Address.AType.PrimaryBusiness, Address.AType.SecondaryBusiness}
                ComboBox1.SelectedIndex = Array.IndexOf(types, a.addressType)
                TextBox1.Text = a.addressLine1
                TextBox2.Text = a.addressLine2
                TextBox3.Text = a.town
                TextBox4.Text = a.city
                TextBox5.Text = a.region
                TextBox6.Text = a.postcode
                TextBox7.Text = a.country
                TextBox8.Text = a.landline
                shareIndex = a.shareIndex
            End If
        End If
 
    End Sub
 
    Private Sub  PictureBox1_Click(ByVal sender As System.Object, ByVal  e As  System.EventArgs) Handles PictureBox1.Click
        Dim nl As String  = Environment.NewLine
        Dim response As MsgBoxResult
        response = MsgBox("Delete this address?" & nl & nl & _
                          "Click OK to delete." & nl & _
                          "Click Cancel to keep.", _
                          MsgBoxStyle.OkCancel Or  MsgBoxStyle.DefaultButton1 Or MsgBoxStyle.Question)
 
        If response = MsgBoxResult.Ok Then
            Dim x As Integer  = Me.Left
            Me.Dispose()
            ''needs removing from collection
            For Each  c As  addressControl In  frmMain.Panel5.Controls.OfType(Of addressControl)()
                If c.Left > x Then
                    c.Left -= 360
                End If
            Next
        End If
         
    End Sub
 
    Public Function  getAddress() As  Address
        Dim a As Address = New Address(DirectCast([Enum].Parse(GetType(Address.AType), ComboBox1.Text.Replace(" ", "")), Address.AType), _
                                                TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, _
                                                TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text)
        If shareIndex > 0 Then a.shareIndex = shareIndex
        Return a
    End Function
 
    Private Sub  ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal  e As  System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex = 0 OrElse ComboBox1.SelectedIndex = 1 Then
            Label9.Text = "Address line 1:"
            Label10.Text = "Address line 2:"
        ElseIf ComboBox1.SelectedIndex = 2 OrElse ComboBox1.SelectedIndex = 3 Then
            Label9.Text = "Business name:"
            Label10.Text = "Address line 1:"
        End If
    End Sub
End Class

The contactsControl

↑ Back to top

Public Class  contactsControl
 
    Private Sub  Button2_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button2.Click
        If ComboBox1.SelectedIndex = -1 Then Return
        ComboBox1.Items.RemoveAt(ComboBox1.SelectedIndex)
        ComboBox1.SelectedIndex = Math.Min(0, ComboBox1.Items.Count - 1)
    End Sub
 
    Private Sub  Button1_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox1.Text)
        ComboBox1.SelectedIndex = ComboBox1.Items.Count - 1
        TextBox1.Text = ""
    End Sub
 
    Public Sub  setDetails(ByVal  p As  Person)
        ComboBox1.Items.Clear()
        ComboBox1.Items.AddRange(p.secondaryPhones.ToArray)
        ComboBox1.SelectedIndex = Math.Min(0, ComboBox1.Items.Count - 1)
        ComboBox2.Items.Clear()
        ComboBox2.Items.AddRange(p.emailAddresses.ToArray)
        ComboBox2.SelectedIndex = Math.Min(0, ComboBox2.Items.Count - 1)
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub
 
    Public Function  getPhoneNumbers() As  List(Of String)
        Return New  List(Of String)(ComboBox1.Items.Cast(Of String).ToArray)
    End Function
 
    Public Function  getEmailAddresses() As List(Of String)
        Return New  List(Of String)(ComboBox2.Items.Cast(Of String).ToArray)
    End Function
 
    Private Sub  Button4_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button4.Click
        ComboBox2.Items.Add(TextBox2.Text)
        ComboBox2.SelectedIndex = ComboBox2.Items.Count - 1
        TextBox2.Text = ""
    End Sub
 
    Private Sub  Button3_Click(ByVal  sender As  System.Object, ByVal  e As  System.EventArgs) Handles Button3.Click
        If ComboBox2.SelectedIndex = -1 Then Return
        ComboBox2.Items.RemoveAt(ComboBox2.SelectedIndex)
        ComboBox2.SelectedIndex = Math.Min(0, ComboBox2.Items.Count - 1)
    End Sub
 
End Class

↑ Back to top


Other Resources

Download here...


References

Ambler, S.W. (Date unknown) Introduction to Object-Orientation and the UML, [Online], 
Available: http://www.agiledata.org/essays/objectOrientation101.html [28 Aug 2016].