Public Class vehiclesForm
    'module level declarations
    Private addBoolean As Boolean = False
    Private editBoolean As Boolean = False
    Private closingBoolean As Boolean = False
    Private previousSelectedInteger As Integer
    Private setupCompleteBoolean As Boolean = False

    Private aCarsDataSet As CarsDataSet
    Private WithEvents carsizeBindingSource As BindingSource
    Private WithEvents vehicleBindingSource As BindingSource
    Private aCarsDataTier As CarsDataTierClass

    Private Sub vehiclesForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        'query the user to save any unsaved changes

        With Me

            .closingBoolean = True
            If .aCarsDataSet.HasChanges Then
                Dim responseDialogResult As DialogResult
                responseDialogResult = MessageBox.Show("Save Database changes?", "Changed Data", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)
                Select Case responseDialogResult
                    Case Windows.Forms.DialogResult.Yes
                        .saveAllChanges()
                    Case Windows.Forms.DialogResult.Cancel
                        e.Cancel = True
                End Select
            End If

        End With
    End Sub

    Private Sub vehiclesForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'instantiate the data tier and fill the DataSet
        Try
            With Me
                aCarsDataTier = New CarsDataTierClass
                .aCarsDataSet = aCarsDataTier.GetDataSet
                'Set up the Car Size Binding Source
                .carsizeBindingSource = New BindingSource
                With .carsizeBindingSource
                    .DataSource = Me.aCarsDataSet
                    .DataMember = "CarSize"
                    .Sort = "SizeCode"
                End With

                'bind the form controls
                With .SizeCodeComboBox
                    .DataSource = Me.carsizeBindingSource
                    .DisplayMember = "SizeCode"
                    .ValueMember = "SizeCode"
                    .DataBindings.Add("text", carsizeBindingSource, "SizeCode", False, DataSourceUpdateMode.Never)
                End With
                .SizeTextBox.DataBindings.Add("text", .carsizeBindingSource, "Size", False, DataSourceUpdateMode.OnValidation)
                .DailyRateTextBox.DataBindings.Add("text", .carsizeBindingSource, "DailyRate", False, DataSourceUpdateMode.OnValidation)
                .MileageRateTextBox.DataBindings.Add("text", .carsizeBindingSource, "MileageRate", False, DataSourceUpdateMode.OnValidation)
                .onReadOnly()

                'set up the vehicle binding source
                .vehicleBindingSource = New BindingSource
                With .vehicleBindingSource
                    .DataSource = Me.carsizeBindingSource
                    .DataMember = "CarSizeToVehiclesRelation"
                End With

                'bind and format the grid
                .VehicleDataGridView.DataSource = .vehicleBindingSource
                .setUpGridColumns()
                .filterTheGrid()
                .setupCompleteBoolean = True
            End With

        Catch ex As Exception
            MessageBox.Show("Error Creating the Data Tier Component " & ex.Message)
        End Try

    End Sub
    Private Sub onReadOnly()

        Me.SizeTextBox.ReadOnly = True
        Me.DailyRateTextBox.ReadOnly = True
        Me.MileageRateTextBox.ReadOnly = True

    End Sub
    Private Sub offReadOnly()

        Me.SizeTextBox.ReadOnly = False
        Me.DailyRateTextBox.ReadOnly = False
        Me.MileageRateTextBox.ReadOnly = False

    End Sub
    Private Sub readyComboBoxBinding()
        'adjust combobox settings to enable add and edit and restores for navigation
        'set combo box to allow use of textbox for 
        If (addBoolean = True Or editBoolean = True) Then
            Me.SizeCodeComboBox.DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation
            Me.SizeCodeComboBox.DropDownStyle = ComboBoxStyle.Simple

        Else 'set combo box to normal for navigation
            Me.SizeCodeComboBox.DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.Never
            Me.SizeCodeComboBox.DropDownStyle = ComboBoxStyle.DropDown
        End If
    End Sub

    Private Sub PhoneComboBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles SizeCodeComboBox.KeyDown
        'procedure handles user hit on Esc by calling the cancel coded herein
        If (Me.addBoolean = True Or Me.editBoolean = True) And e.KeyData = Keys.Escape Then
            Me.deleteCancelButton_Click(sender, e)
        End If
    End Sub

    Private Sub deleteCancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteCancelButton.Click
        If Me.deleteCancelButton.Text = "&Delete" Then
            Dim myDialogResult As DialogResult
            myDialogResult = MessageBox.Show("Confirm deletion!", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
            If myDialogResult = Windows.Forms.DialogResult.Yes Then
                Me.CarSizeBindingSource.RemoveCurrent()
                Call CarSizeBindingSource_PositionChanged(sender, e)
            End If
            'Me.deleteCancelButton.Text = "&Cancel"
        Else
            Me.CarSizeBindingSource.CancelEdit()
            addBoolean = False
            editBoolean = False
            Call readyComboBoxBinding()
            Me.onReadOnly()
            Me.addSaveButton.Text = "&Add"
            Me.editButton.Enabled = True
            Me.deleteCancelButton.Text = "&Delete"
        End If
    End Sub

    Private Sub editButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editButton.Click
        editBoolean = True
        Call readyComboBoxBinding()
        offReadOnly()
        Me.addSaveButton.Text = "&Save"
        Me.deleteCancelButton.Text = "&Cancel"
        Me.editButton.Enabled = False
    End Sub

    Private Sub addSaveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addSaveButton.Click
        If addSaveButton.Text = "&Add" Then
            addBoolean = True
            Call readyComboBoxBinding()
            Me.offReadOnly()
            Me.SizeCodeComboBox.Focus()
            Me.CarSizeBindingSource.AddNew()
            Me.editButton.Enabled = False
            Me.deleteCancelButton.Text = "&Cancel"
            Me.addSaveButton.Text = "&Save"
        Else 'will do save here
            Me.CarSizeBindingSource.EndEdit()
            'be sure these are set to false before setting the combo box back to normal for navigation
            addBoolean = False
            editBoolean = False
            Call readyComboBoxBinding()
            Call Me.onReadOnly()
            Me.addSaveButton.Text = "&Add"
            Me.deleteCancelButton.Text = "&Delete"
            Me.editButton.Enabled = True

        End If

    End Sub

    Private Sub sizeGroupBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sizeGroupBox.Enter

    End Sub

    Private Sub CarSizeBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        With Me.carsizeBindingSource
            recordLabel.Text = "Record " & (.Position + 1).ToString & " of " & .Count.ToString
        End With
    End Sub
    Public Sub saveAllChanges()
        With Me
            'If Me.CarsDataSet.HasChanges = True Then
            Try
                Me.Validate()
                .carsizeBindingSource.EndEdit()
                .vehicleBindingSource.EndEdit()
                .aCarsDataTier.UpdateDataSet(.aCarsDataSet)
                .aCarsDataSet.AcceptChanges()

            Catch MyErr As Exception
                MessageBox.Show(MyErr.Message, "Database Update Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

            'End If
        End With
        
    End Sub

    Private Sub setUpGridColumns()
        'setup the columns for the grid.
        Try
            With Me.VehicleDataGridView
                .Columns!Inv_id.HeaderText = "Inventory Id"
                .Columns!Manuf_Name.HeaderText = "Manufactor's Name"
                .Columns!Model.HeaderText = "Model"
                .Columns!CarSize.HeaderText = "Car Size"
                Dim titleComboBoxColumn As New DataGridViewComboBoxColumn
            End With
        Catch ex As Exception
            MessageBox.Show("Error setting up the grid." & ex.Message)
        End Try
    End Sub
    Private Sub FiltertheGrid()
        'retrieve vehicles for the selected car size
        Dim CarSizeIDString As String
        CarSizeIDString = Me.SizeCodeComboBox.SelectedValue.ToString
        vehicleBindingSource.Filter = "CarSize = '" & CarSizeIDString & "'"
    End Sub

    Private Sub SizeCodeComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SizeCodeComboBox.SelectedIndexChanged
        With Me
            If setupCompleteBoolean Then
                .FiltertheGrid()
            End If
        End With
        
    End Sub

    Private Sub recordLabel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles recordLabel.Click

    End Sub
End Class
