Public Class frmCalendar
    Private Shared _Instance As frmCalendar = Nothing
    Public Shared Function Instance() As frmCalendar
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmCalendar
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function

    Dim XMLPath = System.IO.Path.Combine(Application.UserAppDataPath(), "CALENDAR-UserLayout.xml")

    Private Sub frmCalendar_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        SaveCalendarLayout()
    End Sub

    Private Sub SchedulerControl1_ActiveViewChanged(sender As Object, e As EventArgs) Handles SchedulerControl1.ActiveViewChanged
        SaveCalendarLayout()
    End Sub

    Private Sub frmCalendar_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.TBPMO_APPOINTMENTSTableAdapter.Connection.ConnectionString = MyConnectionString
        Me.TBPMO_RESOURCESTableAdapter.Connection.ConnectionString = MyConnectionString

        Me.TBPMO_RESOURCESTableAdapter.Fill(Me.DD_DMSDataSetCalendar.TBPMO_RESOURCES)
        Me.TBPMO_APPOINTMENTSTableAdapter.Fill(Me.DD_DMSDataSetCalendar.TBPMO_APPOINTMENTS)

        LoadCalendarLayout()
    End Sub

    Private Sub AppointmentsInsertedChangedDeleted(sender As Object, e As DevExpress.XtraScheduler.PersistentObjectsEventArgs) Handles SchedulerStorage1.AppointmentsInserted, SchedulerStorage1.AppointmentsChanged, SchedulerStorage1.AppointmentsDeleted
        Try
            Me.TBPMO_APPOINTMENTSTableAdapter.Connection.ConnectionString = MyConnectionString
            TBPMO_APPOINTMENTSTableAdapter.Update(DD_DMSDataSetCalendar)
            DD_DMSDataSetCalendar.AcceptChanges()
        Catch ex As Exception
            MsgBox("Error in Appointment InsertedChangedDeleted: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        
    End Sub

    Private Sub RessourcesInsertedChangedDeleted(sender As Object, e As DevExpress.XtraScheduler.PersistentObjectsEventArgs) Handles SchedulerStorage1.ResourcesInserted, SchedulerStorage1.ResourcesChanged, SchedulerStorage1.ResourcesDeleted
        Try
            Me.TBPMO_RESOURCESTableAdapter.Connection.ConnectionString = MyConnectionString
            TBPMO_RESOURCESTableAdapter.Update(DD_DMSDataSetCalendar)
            DD_DMSDataSetCalendar.AcceptChanges()
        Catch ex As Exception
            MsgBox("Error in Ressources InsertedChangedDeleted: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub SaveCalendarLayout()
        Try
            SchedulerControl1.SaveLayoutToXml(XMLPath)
        Catch ex As Exception
            MsgBox("Das Kalender-Layout konnte nicht gespeichert werden:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub LoadCalendarLayout()
        Try
            SchedulerControl1.RestoreLayoutFromXml(XMLPath)
        Catch notFoundEx As System.IO.FileNotFoundException
            MsgBox("Das Kalender Layout konnte nicht gefunden werden. Es wird beim Schließen der Form angelegt.", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox("Das Kalender Layout konnte nicht geladen werden:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub SchedulerControl1_EditAppointmentFormShowing(sender As Object, e As DevExpress.XtraScheduler.AppointmentFormEventArgs) Handles SchedulerControl1.EditAppointmentFormShowing
        Dim scheduler As DevExpress.XtraScheduler.SchedulerControl = CType(sender, DevExpress.XtraScheduler.SchedulerControl)
        Dim form As DD_Record_Organiser.frmCustomAppointment = New DD_Record_Organiser.frmCustomAppointment(scheduler, e.Appointment, e.OpenRecurrenceForm)
        Try
            e.DialogResult = form.ShowDialog
            e.Handled = True
        Finally
            form.Dispose()
        End Try
    End Sub
End Class