FormEntry.vb

 Imports System
Imports System.Collections.Generic
imports System.ComponentModel
imports System.Data
imports System.Drawing
imports System.Text
imports System.Windows.Forms
imports System.IO

Public Class FormEntry

#Region "Variables/Objects"

    Private contact As Contacts = New Contacts()
    Private isNewEntry As Boolean = True
    Private fileStream As FileStream
    Private fileName As String = ""
    Private fileSize As Integer = 0
    Private fileRaw() As Byte = New Byte(0) {}

#End Region

#Region "Methods"

    Public Sub loadOptions(ByVal ID As Integer, ByVal isNewEntry As Boolean)
        Me.isNewEntry = isNewEntry
        Me.clearImage(False)
        Main.SetAutoCompleteSource("Contacts", "FirstName", Me.txtFirstName)
        Main.SetAutoCompleteSource("Contacts", "MiddleName", Me.txtMiddleName)
        Main.SetAutoCompleteSource("Contacts", "LastName", Me.txtLastName)

        If isNewEntry Then

            'Set form controls and properties
            Me.Header1.Title = "Add Entry"
            Me.txtID.Text = "[Auto-Generated]"
            Me.txtFirstName.Clear()
            Me.txtMiddleName.Clear()
            Me.txtLastName.Clear()
            Me.txtCellNo.Clear()
            Me.picBox.Image = My.Resources.NoPix
            Me.ShowDialog()
        Else
           'Set values
            Me.Header1.Title = "Update Entry"
            Me.contact = New Contacts(ID)
            If Me.contact.ID = 0 Then
                If MessageBox.Show("The selected contact has been already deleted by other user. " & vbCrLf & vbCrLf & _
                            "Do you want to refresh the list anyway?", "Data Not Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    FormPhoneBook.ShowContacts(True, "")
                    Exit Sub
                End If
                Exit Sub
            End If
        End If

        Me.txtID.Text = Me.contact.ID.ToString().PadLeft(5, "0")
        Me.txtFirstName.Text = Me.contact.FirstName
        Me.txtMiddleName.Text = Me.contact.MiddleName
        Me.txtLastName.Text = Me.contact.LastName
        Me.txtCellNo.Text = Me.contact.CellNo
        Me.fileName = Me.contact.FileName
        Me.fileSize = Me.contact.FileSize
        Me.fileRaw = Me.contact.FileRaw
        Me.picBox.Image = Me.contact.Picture

        If Me.fileName = "" Then
            Me.conRemove.Enabled = False
        Else
            Me.conRemove.Enabled = True

        End If

        Me.ShowDialog()
    End Sub
      
    Private Sub clearImage(ByVal isBrowse As Boolean)
        Me.fileName = ""
        Me.fileSize = 0
        Me.fileRaw = New Byte(0) {}
        Me.picBox.Image = My.Resources.NoPix
        Me.conRemove.Enabled = False
        If isBrowse = False Then Me.picBox.Image = My.Resources.NoPix
    End Sub

    Private Function hasImage() As Boolean
        If Me.fileName <> "" Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub browseImage(ByVal sender As Object, ByVal e As EventArgs) Handles picBox.DoubleClick
        Me.OpenFileDialog1.Title = "Browse for picture"
        Me.OpenFileDialog1.Filter = "All Supported Types|*.jpg;*.jpeg;*.png;*.gif;*.bmp|JPEG Files (*.jpg, *.jpeg)|*.jpg;*.jpeg|PNG Files (*.png)|*.png|GIF Files (*.gif)|* .gif|Bitmap Files (*.bmp)|*.bmp" ';//|All Files (*.*)|*.*"
        Me.OpenFileDialog1.ShowHelp = True
        Me.OpenFileDialog1.ShowDialog(Me)
    End Sub
#End Region

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Me.txtFirstName.Text = "" Then
            MessageBox.Show("Please enter first name.", InfoMessage.REQUIRED, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtFirstName.Focus()
        ElseIf (Me.txtLastName.Text = "") Then
            MessageBox.Show("Please enter last name.", InfoMessage.REQUIRED, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtLastName.Focus()
        ElseIf (Me.txtCellNo.Text = "") Then
            MessageBox.Show("Please enter phone no.", InfoMessage.REQUIRED, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtCellNo.Focus()
        Else
            'Set values
            'Uncomment Me line if you want to try Me method
            'Me.contact = new PhoneBook.Classes.Contacts(Me.txtFirstName.Text, Me.txtMiddleName.Text, Me.txtLastName.Text, Me.txtCellNo.Text, Me.fileName, Me.fileSize, Me.fileRaw);

            'Set values
            If Me.isNewEntry = False Then Me.contact.ID = Convert.ToInt32(Me.txtID.Text)
            Me.contact.FirstName = Me.txtFirstName.Text
            Me.contact.MiddleName = Me.txtMiddleName.Text
            Me.contact.LastName = Me.txtLastName.Text
            Me.contact.CellNo = Me.txtCellNo.Text
            Me.contact.FileName = Me.fileName
            Me.contact.FileSize = Me.fileSize
            Me.contact.FileRaw = Me.fileRaw

            If Me.isNewEntry = True Then
                Me.contact.InsertData()
            Else
                Me.contact.EditData()
            End If
        End If
    End Sub

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        If e.Cancel Then
            Me.clearImage(True)
        Else
            Me.fileStream = New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
            Me.fileSize = Convert.ToInt32(Me.fileStream.Length)

            If Me.fileSize > 2048000 Then

                MessageBox.Show("Image must be less than 500x500 pixels and less than 2Mb file size.", "Image Size", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                e.Cancel = True
                Exit Sub
            End If
            e.Cancel = False
            Me.picBox.Image = Image.FromFile(OpenFileDialog1.FileName)
            Me.fileName = OpenFileDialog1.FileName
            Me.fileRaw = New Byte(fileSize) {}
            Me.fileStream.Read(Me.fileRaw, 0, Me.fileSize)
            Me.fileStream.Close()
            Me.fileStream.Dispose()
            Me.conRemove.Enabled = True
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Dispose()
    End Sub
End Class

Project Homepage: