Public Class vehicleForm
    Private Sub vehicleForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = False
        If CarsDataSet.HasChanges Then
            Dim answerDialogResult As DialogResult
            answerDialogResult = MessageBox.Show("Do you wish to save your changes?", "Save Changes?", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
            If answerDialogResult = Windows.Forms.DialogResult.Yes Then
                Call saveDBChanges()
            End If
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CarsDataSet.Vehicle' table. You can move, or remove it, as needed.
        Me.VehicleTableAdapter.Fill(Me.CarsDataSet.Vehicle)
        Call VehicleBindingSource_PositionChanged(sender, e)
    End Sub

    Private Sub firstButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles firstButton.Click
        With VehicleBindingSource
            If .Position > 0 Then
                .MoveFirst()
            End If
        End With
    End Sub

    Private Sub lastButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lastButton.Click
        With VehicleBindingSource
            If .Position < (.Count - 1) Then
                .MoveLast()
            End If
        End With
    End Sub

    Private Sub previousButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles previousButton.Click
        With VehicleBindingSource
            If .Position > 0 Then
                .MovePrevious()
            Else
                .MoveLast()
            End If
        End With
    End Sub

    Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click
        With VehicleBindingSource
            If .Position < (.Count - 1) Then
                .MoveNext()
            Else
                .MoveFirst()
            End If
        End With
    End Sub
    Private Sub onReadOnly()
        Me.Manuf_NameTextBox.ReadOnly = True
        Inv_IDComboBox.DropDownStyle = ComboBoxStyle.DropDownList
        Inv_IDComboBox.DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.Never
        Me.ModelTextBox.ReadOnly = True
        Me.CarSizeTextBox.ReadOnly = True
    End Sub
    Private Sub offReadOnly()
        Me.Manuf_NameTextBox.ReadOnly = False
        Inv_IDComboBox.DropDownStyle = ComboBoxStyle.Simple
        Inv_IDComboBox.DataBindings!text.DataSourceUpdateMode = DataSourceUpdateMode.OnValidation
        Me.ModelTextBox.ReadOnly = False
        Me.CarSizeTextBox.ReadOnly = False
    End Sub

    Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click
        Call offReadOnly()
        Me.navigateGroupBox.Enabled = False
        Me.VehicleBindingSource.AddNew()
        Me.saveButton.Enabled = True
        Me.deleteButton.Enabled = False
        Me.addButton.Enabled = False
        Inv_IDComboBox.Focus()
        editButton.Text = "&Cancel"
    End Sub
    Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
        Me.VehicleBindingSource.EndEdit()
        Me.editButton.Text = "&Edit"
        Me.saveButton.Enabled = False
        Call onReadOnly()
        Me.navigateGroupBox.Enabled = True
        Me.addButton.Enabled = True
        Me.deleteButton.Enabled = True
    End Sub

    Private Sub editButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editButton.Click
        If editButton.Text = "&Edit" Then
            Call offReadOnly()
            Me.navigateGroupBox.Enabled = False
            Me.saveButton.Enabled = True
            Me.deleteButton.Enabled = False
            Me.addButton.Enabled = False
            Inv_IDComboBox.Focus()
            editButton.Text = "&Cancel"
        Else
            Me.VehicleBindingSource.CancelEdit()
            editButton.Text = "&Edit"
            Call onReadOnly()
            ErrorProvider.Clear()
            Me.navigateGroupBox.Enabled = True
            Me.saveButton.Enabled = False
            Me.deleteButton.Enabled = True
            Me.addButton.Enabled = True
            
        End If
    End Sub
    Sub saveDBChanges()
        If editButton.Text <> "&Cancel" Then 'to prevent saving of an incomplete record (edit or add in process)
            Try
                Me.VehicleBindingSource.CancelEdit() 'edit canceled rather than ended because of possibility of incomplete record being saved.
                Me.VehicleTableAdapter.Update(Me.CarsDataSet.Vehicle)
                Me.CarsDataSet.AcceptChanges()
            Catch MyErr As Exception
                MessageBox.Show("The following DB Error occurred when saving: " & MyErr.Message, "Database Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        Else
            MessageBox.Show("Complete your changes to the record before you save to the table.", "Finish Changes First", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        End If

    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Call saveDBChanges()

    End Sub

    Private Sub deleteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deleteButton.Click
        With Me.VehicleBindingSource
            If .Count <> 0 Then
                .RemoveCurrent()
                Call VehicleBindingSource_PositionChanged(sender, e)
            Else
                MessageBox.Show("There are no records to delete--dataset is empty.", "Empty Table", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            End If
        End With
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub VehicleBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VehicleBindingSource.CurrentChanged

    End Sub

    Private Sub VehicleBindingSource_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles VehicleBindingSource.PositionChanged
        Dim recordNumberShort As Short
        Dim recordCountShort As Short

        With VehicleBindingSource
            recordNumberShort = .Position
            recordCountShort = .Count
            recordDetailsLabel.Text = (recordNumberShort + 1).ToString & " of " & recordCountShort.ToString
        End With
    End Sub

    Private Sub Inv_IDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Manuf_NameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Manuf_NameTextBox.TextChanged

    End Sub

    Private Sub Manuf_NameTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Manuf_NameTextBox.Validating
        With Manuf_NameTextBox
            If .Text = "" Then
                ErrorProvider.SetError(Manuf_NameTextBox, "Required field, Manuf Name.")
                e.Cancel = True
            Else
                ErrorProvider.Clear()
            End If
        End With
    End Sub

    Private Sub ModelTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModelTextBox.TextChanged
        
    End Sub

    Private Sub ModelTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles ModelTextBox.Validating
        With ModelTextBox
            If .Text = "" Then
                ErrorProvider.SetError(ModelTextBox, "Required field, Model.")
                e.Cancel = True
            Else
                ErrorProvider.Clear()
            End If
        End With
    End Sub

    Private Sub CarSizeTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CarSizeTextBox.TextChanged

    End Sub

    Private Sub CarSizeTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles CarSizeTextBox.Validating
        With CarSizeTextBox
            If .Text = "" Then
                ErrorProvider.SetError(CarSizeTextBox, "Required field, Car Size.")
                e.Cancel = True
            Else
                ErrorProvider.Clear()
                Me.saveButton.Focus()
            End If
        End With
    End Sub

    Private Sub Inv_IDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Inv_IDComboBox.SelectedIndexChanged

    End Sub

    Private Sub Inv_IDComboBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Inv_IDComboBox.Validating
        With Inv_IDComboBox
            If .Text = "" Then
                ErrorProvider.SetError(Inv_IDComboBox, "Required field (key field), Inventory ID.")
                e.Cancel = True
            Else
                ErrorProvider.Clear()
            End If
        End With
    End Sub
End Class
