Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '------------------------------------------------------------------------------------------
- ' Notice of My Copyright and Intellectual Property Rights
- '
- ' Any intellectual property contained within the program by Joseph L. Bolen remains the
- ' intellectual property of the Joseph L. Bolen. This means that no person may distribute,
- ' publish or provide such intellectual property to any other person or entity for any
- ' reason, commercial or otherwise, without the express written permission of Joseph L. Bolen.
- '
- ' Copyright © 2016. All rights reserved.
- ' All trademarks remain the property of their respective owners.
- '-------------------------------------------------------------------------------------------
- ' Program Name: Non Database Demo
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 20 JUL 2016
- '
- ' Description: A non-database Contact Maintenance Demo using the BindingList(of T)
- ' with a DataGridView and file serialization to persist data. Demo shows
- ' how to do both binary and XML file I/O processing.
- '
- ' Documentation is at:
- ' App's screen image is at: http://imgur.com/lhW13gs
- ' App's Visual Basic .NET code is at http://pastebin.com/76e6VwXN
- ' Video tutorial at YouTube: http://www.youtube.com/user/bolenpresents
- '-------------------------------------------------------------------------------------------
- Imports System.ComponentModel
- Imports System.IO
- Imports System.Runtime.Serialization.Formatters.Binary
- Imports System.Security
- Imports System.Xml.Serialization
- Public Class MainForm
- #Region " Class Level Variables Instantiated"
- Const BIN_DATA_FILE As String = "contacts.bin"
- Const XML_DATA_FILE As String = "contacts.xml"
- Private contacts As New BindingList(Of Contact)
- Private highWater As Integer
- Private binSwitch As Boolean = True
- #End Region
- #Region " Form Events"
- ' Perform initialization and housekeeping activities prior to showing form.
- Private Sub MainForm_Load(sender As Object, e As EventArgs) _
- Handles MyBase.Load
- 'Bold DataGrid Column Headers.
- ContactDataGridView.ColumnHeadersDefaultCellStyle.Font =
- New Font(ContactDataGridView.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
- ' Load contacts from binary or XML file.
- If binSwitch Then
- BinLoadContacts()
- Else
- XmlLoadContacts()
- End If
- ' Assign contacts BindingList to Binding Source.
- ContactBindingSource.DataSource = contacts
- ' Get highest ContactId value in contacts
- For Each item In contacts
- If highWater < item.ContactId Then
- highWater = item.ContactId
- End If
- Next
- End Sub
- ' Before ending program, save contacts to binary file.
- Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) _
- Handles Me.FormClosing
- ' Save contacts to binary or XML file.
- If binSwitch Then
- BinSaveContacts()
- Else
- XmlSaveContacts()
- End If
- End Sub
- ' Validate and save changes within DataGridView.
- Private Sub ContactBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) _
- Handles ContactBindingNavigatorSaveItem.Click
- Validate()
- ' Update ContactId for new records.
- For Each newContact In contacts.Where(Function(c) c.ContactId = 0)
- highWater += 1
- newContact.ContactId = highWater
- Next
- ContactBindingSource.EndEdit()
- ' Refresh DataGridView after edits.
- ContactBindingSource.ResetBindings(False)
- End Sub
- ' Validate that all cells in DataGrid meet requirements.
- Private Sub ContactDataGridView_CellValidating(sender As Object, e As DataGridViewCellValidatingEventArgs) _
- Handles ContactDataGridView.CellValidating
- ' Validate that LastName is not empty.
- If ContactDataGridView.Columns(e.ColumnIndex).DataPropertyName = "LastName" Then
- If (String.IsNullOrWhiteSpace(e.FormattedValue.ToString())) Then
- ContactDataGridView.Rows(e.RowIndex).ErrorText =
- "Last Name is a required value."
- e.Cancel = True
- End If
- End If
- End Sub
- ' Clear the row error in case the user presses ESC key.
- Private Sub ContactDataGridView_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) _
- Handles ContactDataGridView.CellEndEdit
- ContactDataGridView.Rows(e.RowIndex).ErrorText = String.Empty
- End Sub
- #End Region
- #Region " File I/O Processing"
- ' Load Contacts BindingList from a binary file.
- Private Sub BinLoadContacts()
- Try
- Using stream As Stream = File.Open(BIN_DATA_FILE, FileMode.OpenOrCreate)
- Dim bin As New BinaryFormatter()
- If stream.Length > 0 Then
- contacts = CType(bin.Deserialize(stream), BindingList(Of Contact))
- End If
- End Using
- Catch ex As IOException
- MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Sub
- ' Save Contacts BindingList to a binary file.
- Private Sub BinSaveContacts()
- If contacts.Count > 0 Then
- Try
- Using stream As Stream = File.Open(BIN_DATA_FILE, FileMode.Create)
- Dim bin As New BinaryFormatter()
- bin.Serialize(stream, contacts)
- End Using
- Catch ex As IOException
- MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End If
- End Sub
- ' Load Contacts BindingList from a XML file.
- Private Sub XmlLoadContacts()
- Try
- Using stream As Stream = File.Open(XML_DATA_FILE, FileMode.OpenOrCreate)
- Dim xml As New XmlSerializer(contacts.GetType)
- If stream.Length > 0 Then
- contacts = CType(xml.Deserialize(stream), BindingList(Of Contact))
- End If
- End Using
- Catch ex As IOException
- MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End Sub
- ' Save Contacts BindingList to a XML file.
- Private Sub XmlSaveContacts()
- If contacts.Count > 0 Then
- Try
- Using stream As Stream = File.Open(XML_DATA_FILE, FileMode.Create)
- Dim xml As New XmlSerializer(contacts.GetType)
- xml.Serialize(stream, contacts)
- End Using
- Catch ex As IOException
- MessageBox.Show(ex.Message, "Accessing File Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As SecurityException
- MessageBox.Show(ex.Message, "Security Permissions Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- Catch ex As Exception
- MessageBox.Show(ex.Message, ex.GetType.ToString, MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Try
- End If
- End Sub
- #End Region
- End Class
- '-------------------------------------------------------------------------------------------
- ' Class Name: Contact
- '
- ' Author: Joseph L. Bolen
- ' Date Created: 20 JUL 2016
- '
- ' Description: Simple auto-implemented properties for a Contacts list with a default and
- ' one parameterized constructor.
- '-------------------------------------------------------------------------------------------
- <Serializable>
- Public Class Contact
- Public Property ContactId As Integer
- Public Property FirstName As String
- Public Property MiddleName As String
- Public Property LastName As String
- Public Property StreetAddress As String
- Public Property ExtendedAddress As String
- Public Property City As String
- Public Property State As String
- Public Property PostalCode As String
- Public Property CountryCode As String
- Public Property PrimaryTelephone As String
- Public Property SecondaryTelephone As String
- Public Property DateOfBirth As Nullable(Of Date)
- Public Property Gender As String
- Public Property Notes As String
- ' Default Constructor
- Public Sub New()
- MyBase.New
- End Sub
- ' Parameterized Constructor
- Public Sub New(ByVal id As Integer, ByVal fName As String, ByVal lName As String)
- MyBase.New
- ContactId = id
- FirstName = fName
- LastName = lName
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement