Public Class MainForm

    Private Sub MainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            'Fill the CourseTable.
            CourseForm.CourseTableAdapter.Fill(CourseForm.StudentDataDataSet.Course)

        Catch ex As SqlClient.SqlException
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub courseMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles courseMenuItem.Click
        'The Course form is displayed.
        'CourseForm.ShowDialog()
        CourseForm.Show()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        allCoursesComboBox.DataSource = CourseForm.CourseBindingSource
        allCoursesComboBox.DisplayMember = "CourseCode"
    End Sub

    Private Sub showDataButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles showDataButton.Click
        'Load student data for the tutor user. Load results data.
        'Make tutor ID text box read-only.
        Try
            Me.StudentTableAdapter.FillByTutorID(Me.StudentDataDataSet.Student, tutorIdTextBox.Text)
            Me.ResultsTableAdapter.FillByTutorID(Me.StudentDataDataSet.Results, tutorIdTextBox.Text)
            tutorIdTextBox.ReadOnly = True
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub studentMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles studentMenuItem.Click
        'The Student form is displayed. If the user edits the data and clicks on Save,
        'then the Student data is updated; otherwise any new edits are discarded.
        If StudentForm.ShowDialog = Windows.Forms.DialogResult.OK Then
            StudentBindingSource.EndEdit()
        End If
        StudentForm.Show()
    End Sub

    Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
        'The local data set is updated with the amended results record.
        FKResultsStudentBindingSource.EndEdit()
    End Sub

    Private Sub saveMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveMenuItem.Click
        'Update the local Results and Student data. Then update the database.
        Try
            StudentBindingSource.EndEdit()
            FKResultsStudentBindingSource.EndEdit()
            StudentTableAdapter.Update(StudentDataDataSet.Student)
            ResultsTableAdapter.Update(StudentDataDataSet.Results)
        Catch ex As SqlClient.SqlException
            MessageBox.Show(ex.ToString())
        End Try
    End Sub

    Private Sub MainForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        'All data is saved to the database.
        saveMenuItem_Click(sender, e)
    End Sub

    Private Sub clearMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearMenuItem.Click
        'Save current data. Clear data from the Student and Results tables.
        saveMenuItem_Click(sender, e)
        StudentDataDataSet.Student.Clear()
        StudentDataDataSet.Results.Clear()
        tutorIdTextBox.ReadOnly = False
    End Sub

    Private Sub addCourseMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addCourseMenuItem.Click
        'Add selected student-course to Results table in local data set.
        Dim row As DataRowView
        Try
            FKResultsStudentBindingSource.EndEdit()
            row = CType(FKResultsStudentBindingSource.AddNew(), DataRowView)
            row.Item("CourseCode") = allCoursesComboBox.Text
            FKResultsStudentBindingSource.EndEdit()
        Catch ex As Data.ConstraintException
            FKResultsStudentBindingSource.CancelEdit()
            MessageBox.Show("Course was not added - is it already on the student's list?")
        End Try
    End Sub

    Private Sub removeCourseMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles removeCourseMenuItem.Click
        'Remove currently selected student-course from Results table in local data set.
        FKResultsStudentBindingSource.RemoveCurrent()
        FKResultsStudentBindingSource.EndEdit()
    End Sub

    Private Sub exitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitMenuItem.Click
        'The main form is closed
        Close()
    End Sub

    Private Sub cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel_Button.Click
        'All edits made in the results panel since the data was last saved are cancelled.
        FKResultsStudentBindingSource.CancelEdit()
    End Sub

End Class
