#Region " Contact "
#Region " Examples (Normal usage)"
' Create a new list of contacts
' Dim Contacts As List(Of Contact) = New List(Of Contact)
' Or load ContactList from previous serialized file
' Dim Contacts As List(Of Contact) = ContactSerializer.Deserialize("C:\Contacts.bin")
' Set a variable to store the current contact position
' Dim CurrentPosition As Integer = 0
' Create a new contact
' Dim CurrentContact As Contact = New Contact With { _
' .Name = "Manolo", _
' .Surname = "El del Bombo", _
' .Country = "Spain", _
' .City = "Valencia", _
' .Street = "Av. Mestalla", _
' .ZipCode = "42731", _
' .Phone = "96.XXX.XX.XX", _
' .CellPhone = "651.XXX.XXX", _
' .Email = "ManoloToLoko@Gmail.com"}
' Add a contact to contacts list
' Contacts.Add(CurrentContact)
' Update the CurrentPosition index value
' CurrentPosition = Contacts.IndexOf(CurrentContact)
#End Region
#Region " Examples (Generic functions) "
' Examples:
'
' -----------------
' Add a new contact
' -----------------
' Contact.Add_Contact(ContactList, "Manolo", "El del Bombo", "Spain", "Valencia", "Av. Mestalla", "42731", "96.XXX.XX.XX", "651.XXX.XXX", "ManoloToLoko@Gmail.com")
'
'
' -----------------------------------------------------------------
' Load a contact from an existing contacts list into TextBox Fields
' -----------------------------------------------------------------
' Contact.Load_Contact(ContactList, 0, TextBox_Name, textbox_surName, TextBox_Country, textbox_City, TextBox_Street, TextBox_ZipCode, TextBox_Phone, TextBox_CellPhone, TextBox_email)
'
'
' ----------------------------------
' Load a contact into TextBox Fields
' ----------------------------------
' Contact.Load_Contact(Contact, TextBox_Name, textbox_surName, TextBox_Country, textbox_City, TextBox_Street, TextBox_ZipCode, TextBox_Phone, TextBox_CellPhone, TextBox_email)
'
'
' ---------------------------------
' Load a contact list into ListView
' ---------------------------------
' Contact.Load_ContactList_Into_ListView(ContactList, ListView1)
'
'
' -------------------------------------
' Load a contact list into DataGrivView
' -------------------------------------
' Contact.Load_ContactList_Into_DataGrivView(ContactList, DataGrivView1)
'
'
' -------------------------------------------
' Load a contacts list from a serialized file
' -------------------------------------------
' Dim ContactList As List(Of Contact) = Contact.Load_ContactList("C:\Contacts.bin")
'
'
' -----------------------------------------------------------------------
' Find the first occurrence of a contact name in a existing contacts list
' -----------------------------------------------------------------------
' Dim ContactFound As Contact = Contact.Match_Contact_Name_FirstOccurrence(ContactList, "Manolo")
'
'
' ----------------------------------------------------------------------
' Find all the occurrences of a contact name in a existing contacts list
' ----------------------------------------------------------------------
' Dim ContactsFound As List(Of Contact) = Contact.Match_Contact_Name(ContactList, "Manolo")
'
'
' -------------------------------------------------------------
' Remove a contact from a Contact List giving the contact index
' -------------------------------------------------------------
' Remove_Contact(ContactList, 0)
'
'
' -------------------------------------------------------
' Remove a contact from a Contact List giving the contact
' -------------------------------------------------------
' Remove_Contact(ContactList, MyContact)
'
'
' -------------------------
' Save the contacts to file
' -------------------------
' Contact.Save_ContactList(ContactList, "C:\Contacts.bin")
'
'
' -------------------------
' Sort the contacts by name
' -------------------------
' Dim SorteredContacts As List(Of Contact) = Contact.Sort_ContactList_By_Name(ContactList, Contact.ContectSortMode.Ascending)
'
'
' --------------------------------------------------------------------
' Get a formatted string containing the details of an existing contact
' --------------------------------------------------------------------
' MsgBox(Contact.Get_Contact_Details(ContactList, 0))
' MsgBox(Contact.Get_Contact_Details(CurrentContact))
'
'
' ----------------------------------------------------------------------------------
' Copy to clipboard a formatted string containing the details of an existing contact
' ----------------------------------------------------------------------------------
' Contact.Copy_Contact_Details_To_Clipboard(ContactList, 0)
' Contact.Copy_Contact_Details_To_Clipboard(CurrentContact)
#End Region
<Serializable()> _
Public Class Contact
Public Enum ContectSortMode As Short
Ascending = 0
Descending = 1
End Enum
#Region "Member Variables"
Private mId As System.Guid
Private mName As String
Private mSurname As String
Private mCountry As String
Private mCity As String
Private mStreet As String
Private mZip As String
Private mPhone As String
Private mCellPhone As String
Private mEmail As String
#End Region
#Region "Constructor"
Public Sub New()
mId = Guid.NewGuid()
End Sub
Public Sub New(ByVal ID As System.Guid)
mId = ID
End Sub
#End Region
#Region "Properties"
Public Property Name() As String
Get
Return mName
End Get
Set(ByVal value As String)
mName = value
End Set
End Property
Public Property Surname() As String
Get
Return mSurname
End Get
Set(ByVal value As String)
mSurname = value
End Set
End Property
Public Property Street() As String
Get
Return mStreet
End Get
Set(ByVal value As String)
mStreet = value
End Set
End Property
Public Property City() As String
Get
Return mCity
End Get
Set(ByVal value As String)
mCity = value
End Set
End Property
Public Property Country() As String
Get
Return mCountry
End Get
Set(ByVal value As String)
mCountry = value
End Set
End Property
Public Property ZipCode() As String
Get
Return mZip
End Get
Set(ByVal value As String)
mZip = value
End Set
End Property
Public Property Email() As String
Get
Return mEmail
End Get
Set(ByVal value As String)
mEmail = value
End Set
End Property
Public Property Phone() As String
Get
Return mPhone
End Get
Set(ByVal value As String)
mPhone = value
End Set
End Property
Public Property CellPhone() As String
Get
Return mCellPhone
End Get
Set(ByVal value As String)
mCellPhone = value
End Set
End Property
#End Region
#Region " ContactSerializer "
Public Class ContactSerializer
''' <summary>
''' Serialize a contact list into a contacts file.
''' </summary>
''' <param name="ContactList"></param>
''' <param name="FilePath"></param>
''' <remarks></remarks>
Public Shared Sub Save(ByVal ContactList As List(Of Contact), _
ByVal FilePath As String)
Dim fs As IO.FileStream = Nothing
Dim formatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Try
fs = New IO.FileStream(FilePath, IO.FileMode.OpenOrCreate)
formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
formatter.Serialize(fs, ContactList)
Catch ex As Exception
MessageBox.Show(String.Format("{0}:{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace), _
"Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Finally
If fs IsNot Nothing Then fs.Dispose()
End Try
End Sub
''' <summary>
''' Deserialize an existing file into a contact list.
''' </summary>
''' <param name="FilePath"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function Load(ByVal FilePath As String) As List(Of Contact)
Dim fs As IO.FileStream = Nothing
Dim formatter As System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Try
fs = New IO.FileStream(FilePath, IO.FileMode.Open)
formatter = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter()
Return formatter.Deserialize(fs)
Catch ex As Exception
MessageBox.Show(String.Format("{0}:{1}{1}{2}", ex.Message, Environment.NewLine, ex.StackTrace), _
"Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Return Nothing
Finally
If fs IsNot Nothing Then fs.Dispose()
End Try
End Function
End Class
#End Region
#Region " Generic Functions "
' Formatted String of contact detailed information
Shared ReadOnly DetailsFormat As String = _
"Name.....: {1}{0}Surname..: {2}{0}Country..: {3}{0}City.....: {4}{0}Street...: {5}{0}Zipcode..: {6}{0}Phone....: {7}{0}CellPhone: {8}{0}Email....: {9}"
''' <summary>
''' Add a new contact into a existing contacts list.
''' </summary>
Public Shared Sub Add_Contact(ByVal ContactList As List(Of Contact), _
ByVal Name As String, _
ByVal Surname As String, _
ByVal Country As String, _
ByVal City As String, _
ByVal Street As String, _
ByVal ZipCode As String, _
ByVal Phone As String, _
ByVal CellPhone As String, _
ByVal Email As String)
ContactList.Add(New Contact With { _
.Name = Name, _
.Surname = Surname, _
.Country = Country, _
.City = City, _
.Street = Street, _
.ZipCode = ZipCode, _
.Phone = Phone, _
.CellPhone = CellPhone, _
.Email = Email _
})
End Sub
''' <summary>
''' Remove a contact from an existing contacts list.
''' </summary>
Public Shared Sub Remove_Contact(ByVal ContactList As List(Of Contact), ByVal ContactIndex As Integer)
ContactList.RemoveAt(ContactIndex)
End Sub
''' <summary>
''' Remove a contact from an existing contacts list.
''' </summary>
Public Shared Sub Remove_Contact(ByVal ContactList As List(Of Contact), ByVal Contact As Contact)
ContactList.Remove(Contact)
End Sub
''' <summary>
''' Find the first occurrence of a contact name in an existing contacts list.
''' </summary>
Public Shared Function Match_Contact_Name_FirstOccurrence(ByVal ContactList As List(Of Contact), ByVal Name As String) As Contact
Return ContactList.Find(Function(contact) contact.Name.ToLower.StartsWith(Name.ToLower) _
OrElse contact.Name.ToLower.Contains(Name.ToLower))
End Function
''' <summary>
''' Find all the occurrences of a contact name in a existing contacts list.
''' </summary>
Public Shared Function Match_Contact_Name(ByVal ContactList As List(Of Contact), ByVal Name As String) As List(Of Contact)
Return ContactList.FindAll(Function(contact) contact.Name.ToLower.StartsWith(Name.ToLower) _
OrElse contact.Name.ToLower.Contains(Name.ToLower))
End Function
''' <summary>
''' Load a contact from an existing contacts list into textbox fields.
''' </summary>
Public Shared Sub Load_Contact(ByVal ContactList As List(Of Contact), _
ByVal ContactIndex As Integer, _
ByVal TextBox_Name As TextBox, _
ByVal TextBox_Surname As TextBox, _
ByVal TextBox_Country As TextBox, _
ByVal TextBox_City As TextBox, _
ByVal TextBox_Street As TextBox, _
ByVal TextBox_Zipcode As TextBox, _
ByVal TextBox_Phone As TextBox, _
ByVal TextBox_CellPhone As TextBox, _
ByVal TextBox_Email As TextBox)
TextBox_Name.Text = ContactList.Item(ContactIndex).Name
TextBox_Surname.Text = ContactList.Item(ContactIndex).Surname
TextBox_Country.Text = ContactList.Item(ContactIndex).Country
TextBox_City.Text = ContactList.Item(ContactIndex).City
TextBox_Street.Text = ContactList.Item(ContactIndex).Street
TextBox_Zipcode.Text = ContactList.Item(ContactIndex).ZipCode
TextBox_Phone.Text = ContactList.Item(ContactIndex).Phone
TextBox_CellPhone.Text = ContactList.Item(ContactIndex).CellPhone
TextBox_Email.Text = ContactList.Item(ContactIndex).Email
End Sub
''' <summary>
''' Load a contact into textbox fields.
''' </summary>
Public Shared Sub Load_Contact(ByVal Contact As Contact, _
ByVal TextBox_Name As TextBox, _
ByVal TextBox_Surname As TextBox, _
ByVal TextBox_Country As TextBox, _
ByVal TextBox_City As TextBox, _
ByVal TextBox_Street As TextBox, _
ByVal TextBox_Zipcode As TextBox, _
ByVal TextBox_Phone As TextBox, _
ByVal TextBox_CellPhone As TextBox, _
ByVal TextBox_Email As TextBox)
TextBox_Name.Text = Contact.Name
TextBox_Surname.Text = Contact.Surname
TextBox_Country.Text = Contact.Country
TextBox_City.Text = Contact.City
TextBox_Street.Text = Contact.Street
TextBox_Zipcode.Text = Contact.ZipCode
TextBox_Phone.Text = Contact.Phone
TextBox_CellPhone.Text = Contact.CellPhone
TextBox_Email.Text = Contact.Email
End Sub
''' <summary>
''' Seriale a contacts list to a file.
''' </summary>
Public Shared Sub Save_ContactList(ByVal ContactList As List(Of Contact), ByVal FilePath As String)
Contact.ContactSerializer.Save(ContactList, FilePath)
End Sub
''' <summary>
''' Load a contacts list from a serialized file.
''' </summary>
Public Shared Function Load_ContactList(ByVal FilePath As String) As List(Of Contact)
Return Contact.ContactSerializer.Load(FilePath)
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Name field.
''' </summary>
Public Shared Function Sort_ContactList_By_Name(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Name).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Name).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Surname field.
''' </summary>
Public Shared Function Sort_ContactList_By_Surname(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Surname).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Surname).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Country field.
''' </summary>
Public Shared Function Sort_ContactList_By_Country(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Country).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Country).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the City field.
''' </summary>
Public Shared Function Sort_ContactList_By_City(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.City).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.City).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Street field.
''' </summary>
Public Shared Function Sort_ContactList_By_Street(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Street).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Street).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Zipcode field.
''' </summary>
Public Shared Function Sort_ContactList_By_Zipcode(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.ZipCode).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.ZipCode).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Phone field.
''' </summary>
Public Shared Function Sort_ContactList_By_Phone(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Phone).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Phone).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the CellPhone field.
''' </summary>
Public Shared Function Sort_ContactList_By_CellPhone(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.CellPhone).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.CellPhone).ToList())
End Function
''' <summary>
''' Reorder the contacts of a Contacts List by the Email field.
''' </summary>
Public Shared Function Sort_ContactList_By_Email(ByVal ContactList As List(Of Contact), _
ByVal ContectSortMode As Contact.ContectSortMode) As List(Of Contact)
Return If(ContectSortMode = Contact.ContectSortMode.Ascending, _
ContactList.OrderBy(Function(contact) contact.Email).ToList(), _
ContactList.OrderByDescending(Function(contact) contact.Email).ToList())
End Function
''' <summary>
''' Get a formatted string containing the details of an existing contact.
''' </summary>
Public Shared Function Get_Contact_Details(ByVal ContactList As List(Of Contact), ByVal ContactIndex As Integer) As String
Return String.Format(DetailsFormat, _
Environment.NewLine, _
ContactList.Item(ContactIndex).Name, _
ContactList.Item(ContactIndex).Surname, _
ContactList.Item(ContactIndex).Country, _
ContactList.Item(ContactIndex).City, _
ContactList.Item(ContactIndex).Street, _
ContactList.Item(ContactIndex).ZipCode, _
ContactList.Item(ContactIndex).Phone, _
ContactList.Item(ContactIndex).CellPhone, _
ContactList.Item(ContactIndex).Email)
End Function
''' <summary>
''' Get a formatted string containing the details of an existing contact.
''' </summary>
Public Shared Function Get_Contact_Details(ByVal Contact As Contact) As String
Return String.Format(DetailsFormat, _
Environment.NewLine, _
Contact.Name, _
Contact.Surname, _
Contact.Country, _
Contact.City, _
Contact.Street, _
Contact.ZipCode, _
Contact.Phone, _
Contact.CellPhone, _
Contact.Email)
End Function
''' <summary>
''' Copy to clipboard a formatted string containing the details of an existing contact.
''' </summary>
Public Shared Sub Copy_Contact_Details_To_Clipboard(ByVal ContactList As List(Of Contact), ByVal ContactIndex As Integer)
Clipboard.SetText(String.Format(DetailsFormat, _
Environment.NewLine, _
ContactList.Item(ContactIndex).Name, _
ContactList.Item(ContactIndex).Surname, _
ContactList.Item(ContactIndex).Country, _
ContactList.Item(ContactIndex).City, _
ContactList.Item(ContactIndex).Street, _
ContactList.Item(ContactIndex).ZipCode, _
ContactList.Item(ContactIndex).Phone, _
ContactList.Item(ContactIndex).CellPhone, _
ContactList.Item(ContactIndex).Email))
End Sub
''' <summary>
''' Copy to clipboard a formatted string containing the details of an existing contact.
''' </summary>
Public Shared Sub Copy_Contact_Details_To_Clipboard(ByVal Contact As Contact)
Clipboard.SetText(String.Format(DetailsFormat, _
Environment.NewLine, _
Contact.Name, _
Contact.Surname, _
Contact.Country, _
Contact.City, _
Contact.Street, _
Contact.ZipCode, _
Contact.Phone, _
Contact.CellPhone, _
Contact.Email))
End Sub
''' <summary>
''' Load an existing contacts list into a ListView.
''' </summary>
Public Shared Sub Load_ContactList_Into_ListView(ByVal ContactList As List(Of Contact), _
ByVal Listview As ListView)
Listview.Items.AddRange( _
ContactList _
.Select(Function(Contact) _
New ListViewItem(New String() { _
Contact.Name, _
Contact.Surname, _
Contact.Country, _
Contact.City, _
Contact.Street, _
Contact.ZipCode, _
Contact.Phone, _
Contact.CellPhone, _
Contact.Email _
})).ToArray())
End Sub
''' <summary>
''' Load an existing contacts list into a DataGridView.
''' </summary>
Public Shared Sub Load_ContactList_Into_DataGridView(ByVal ContactList As List(Of Contact), _
ByVal DataGridView As DataGridView)
DataGridView.DataSource = ContactList
' Sortered:
' DataGridView.DataSource = (From Contact In ContactList Order By Contact.Name Ascending Select Contact).ToList
End Sub
#End Region
End Class
#End Region