Imports DevExpress.XtraScheduler
Imports System.IO
Imports WINDREAMLib

Public Class frmQuickEdit



#Region "+++++ Konstanten +++++"
    'Private akt_ctrl As Control
    Private thisformid As Integer = Nothing
    Private SelectedRecordID As Integer = 0
    Private SelectedFormID As Integer = Nothing
    Private m_Moving As Boolean = False
    Private insert = False
    'Private Shared _Instance As frmFormInput = Nothing
    'Public Shared Function Instance() As frmFormInput
    '    If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
    '        _Instance = New frmFormInput
    '    End If
    '    _Instance.BringToFront()
    '    Return _Instance
    'End Function

#End Region


    Public Sub SetFormTitles(title As String)
        Me.Text = title
        '   BindingNavigatorCountItem.Text = BindingNavigatorCountItem.Text.Replace("Datensätzen", title)
    End Sub

#Region "+++++ Form Events +++++"
    Public Sub LoadOverview_Grid()
        Try
            'Load Input Grid
            Dim _sqlGrid As String = "SELECT * FROM VWTEMP_PMO_FORM" & thisformid ' "SELECT SQL_COMMAND FROM TBPMO_FORM_SQL WHERE FORM_ID = " & thisformid
            Dim dt As DataTable = ClassDatabase.Return_Datatable(_sqlGrid)
            If dt Is Nothing = False Then
                If dt.Rows.Count > 0 Then
                    'BindingSource for Navigator
                    Me.BindingSourceDGV.DataSource = dt
                    'bind Datatable to bindingsource
                    Me.dgvInput.DataSource = Me.BindingSourceDGV
                    'Me.dgvOverview.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
                Else
                    MsgBox("No entries for actual Form. Pleas insert the first values.", MsgBoxStyle.Exclamation)
                End If
            End If


        Catch ex As Exception
            MsgBox("Error in Load Overview Grid:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub frmLevel_Designer_Load(sender As Object, e As EventArgs) Handles Me.Load
        Try


            Me.VWPMO_CONTROL_SCREENTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_RECORDTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.VWPMO_CONTROL_SCREENTableAdapter.Fill(Me.DD_DMSDataSet.VWPMO_CONTROL_SCREEN, CURRENT_FORM_ID)


            CtrlBuilder = New ClassControlBuilder(pnlView,
                                     AddressOf Control_MouseDown,
                                     AddressOf Control_MouseUp,
                                     AddressOf Control_MouseMove,
                                     AddressOf Control_Click,
                                     AddressOf Groupbox_DragDrop)

            CtrlCommandUI = New ClassControlCommandsUI(CtrlBuilder,
                                                       ContextMenuStrip1,
                                                       AddressOf NewEditAppointment,
                                                       AddressOf OpenFormData)

            If ClassDatabase.Init = True Then
                LoadOverview_Grid()
                LoadFormProperties()
                'LoadControls()
                CtrlCommandUI.LoadControls(CURRENT_FORM_ID)
                LoadInitialValues()
                FocusedRow_Change()
            End If

            ClassWindowLocation.LoadFormLocationSize(Me, CURRENT_FORM_ID, CURRENT_SCREEN_ID)

        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try



    End Sub

    Private Sub HandleToolTip(sender As Object, e As EventArgs)
        Console.WriteLine("show tooltip")
    End Sub
#End Region

#Region "+++++ Layout +++++"
    Private Sub frmFormInput_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        LoadLayout()
    End Sub

    Private Sub frmLevel_Designer_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            SaveLayout()
            ClassWindowLocation.SaveFormLocationSize(Me, thisformid, CURRENT_SCREEN_ID)
            My.Settings.Save()

            'frmForm_Overview.Instance.Show()
            'frmTool_ControlProperties.Instance.Close()
            'frmTool_ControlDesigner.Instance.Close()
        Catch ex As Exception

        End Try
    End Sub

    Public Sub LoadLayout()

        Try
            'DataGridView Layout laden
            Dim XMLPath = System.IO.Path.Combine(Application.UserAppDataPath(), "SCREEN" & CURRENT_SCREEN_ID & "-FORM" & thisformid & "-UserLayout.xml")
            dgvInput.MainView.RestoreLayoutFromXml(XMLPath)


            'Splitter Layout laden
            Dim LayoutPath = Path.Combine(Application.UserAppDataPath(), "SCREEN" & CURRENT_SCREEN_ID & "-FORM" & thisformid & "-SplitterLayout.xml")

            Dim layout As ClassLayout = New ClassLayout(LayoutPath)
            Dim settings As System.Collections.Generic.List(Of ClassSetting)
            settings = layout.Load()

            If settings.Count = 0 Then
                settings.Add(New ClassSetting("SplitContainerMain", SplitContainerMain.SplitterDistance))
                layout.Save(settings)
            End If

            For Each s As ClassSetting In settings
                Select Case s._name
                    Case "SplitContainerMain"
                        SplitContainerMain.SplitterDistance = Integer.Parse(s._value)
                End Select
            Next
        Catch notFoundEx As System.IO.FileNotFoundException
            ClassLogger.Add("Layout added for Screen " & CURRENT_SCREEN_ID & ", Form " & thisformid)
        Catch ex As Exception
            MsgBox("Error while loading Layout!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub


    Public Sub SaveLayout()
        Try
            'DataGridView Layout speichern
            Dim XMLPath = System.IO.Path.Combine(Application.UserAppDataPath(), "SCREEN" & CURRENT_SCREEN_ID & "-FORM" & thisformid & "-UserLayout.xml")
            dgvInput.MainView.SaveLayoutToXml(XMLPath)

            'Splitter Layout speichern
            Dim LayoutPath = Path.Combine(Application.UserAppDataPath(), "SCREEN" & CURRENT_SCREEN_ID & "-FORM" & thisformid & "-SplitterLayout.xml")

            Dim layout As ClassLayout = New ClassLayout(LayoutPath)
            Dim settings As System.Collections.Generic.List(Of ClassSetting) = New System.Collections.Generic.List(Of ClassSetting)
            settings.Add(New ClassSetting("SplitContainerMain", SplitContainerMain.SplitterDistance))
            layout.Save(settings)
        Catch ex As Exception
            MsgBox("Error while saving Layout!" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub


#End Region

#Region "+++++ Load Funktionen +++++"
    Public Sub LoadFormProperties()
        'Dim NAME_SQL As String = "SELECT NAME FROM TBPMO_FORM WHERE GUID = " & CURRENT_FORM_ID
        Dim TITLE_SQL As String = "SELECT FORM_TITLE FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & thisformid
        'Dim name = ClassDatabase.Execute_Scalar(NAME_SQL)
        Dim title = ClassDatabase.Execute_Scalar(TITLE_SQL)

        'SetFormName(name)
        SetFormTitles("Schnelleingabe - " & title)
    End Sub

    Sub LoadInitialValues()
        Dim controls As System.Windows.Forms.Control.ControlCollection = pnlView.Controls

        For Each control As Control In controls
            Dim CONTROL_ID As Integer = GetControlID_for_RecordID(control.Name, 1)

            If (CONTROL_ID <> -1) Then
                Load_Value(1, CONTROL_ID, control)
            End If
        Next
    End Sub

    'Private Function GetControlID(recID As Integer, control As Control) As Integer
    '    Try
    '        Dim ID As Integer = ClassDatabase.Execute_Scalar("SELECT CONTROL_ID FROM VWPMO_VALUES WHERE RECORD_ID = " & recID & " and CONTROL_NAME = '" & control.Name & "'")
    '        If ID > 0 Then
    '            Return ID
    '        Else
    '            Return -1
    '        End If
    '    Catch ex As Exception
    '        MsgBox("Error in GetControlID:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
    '    End Try
    'End Function

    Private Sub Load_Value(recID As Integer, CONTROLID As Integer, control As Control)
        Try
            Dim sq = "SELECT * FROM VWPMO_VALUES WHERE RECORD_ID = " & recID & " and CONTROL_ID = " & CONTROLID
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sq)
            Dim result = DT.Rows(0).Item("VALUE")
            Select Case DT.Rows(0).Item("CONTROL_TYPE_ID")
                Case 2 ' TextBox
                    Dim textbox As TextBox = DirectCast(control, TextBox)
                    textbox.Text = result
                Case 10 ' CheckBox
                    Dim checkbox As CheckBox = DirectCast(control, CheckBox)
                    checkbox.Checked = CBool(result)
                Case 3 ' ComboBox
                    Dim combobox As ComboBox = DirectCast(control, ComboBox)
                    combobox.SelectedIndex = combobox.FindStringExact(result)
                Case 4 'DateTimePicker
                    Dim datepicker As DevExpress.XtraEditors.DateEdit = DirectCast(control, DevExpress.XtraEditors.DateEdit)
                    datepicker.DateTime = Date.Parse(result)
                Case Else
                    ' MsgBox(DT.Rows(0).Item("CONTROL_TYPE_ID"))
            End Select
        Catch ex As Exception
            MsgBox("Error in Load_Value:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Function Return_SearchPAttern(ByVal content As String)
        Select Case content
            Case "Form-ID (Entity)"
                Return thisformid
            Case "Record-ID"
                Return SelectedRecordID

        End Select
    End Function
#End Region

#Region "+++++ Mouse/Key Events +++++"
    Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

    End Sub
    Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp

    End Sub
    Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

    End Sub

    Private Sub Control_Click(sender As Object, e As EventArgs)
        ' TODO: CLICK CONTROL
    End Sub

    Private Sub Groupbox_DragDrop(sender As Object, e As DragEventArgs)
        ' TODO: DRAG DROP GROUPBOX
    End Sub

#End Region

    Sub OpenFormData(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim ctrl As Control = sender
        Cursor = Cursors.WaitCursor
        ClassFunctionCommandsUI.OpenFormData(ctrl.Name, thisformid)
        Cursor = Cursors.Default
    End Sub

    Sub NewEditAppointment(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim ctrl As Control = sender
        ClassFunctionCommandsUI.NewEditAppointment(ctrl.Name, thisformid, SelectedRecordID, pnlView.Controls)
    End Sub

    Private Sub FocusedRow_Change()
        Try
            If gvMainView.Columns.Count = 0 Then
                Exit Sub
            End If
            Dim RecordID = gvMainView.GetFocusedRowCellValue(gvMainView.Columns(0))
            If RecordID Is Nothing Then
                Exit Sub
            End If
            Dim CONTROL_ID As Integer
            If RecordID Is Nothing = False Then
                If (Not IsDBNull(RecordID)) Then
                    ' TODO: AUCH IN CONTSTRUCTOR
                    ' TODO: Doppelte einträge bei INSERT und UPDATE fixen
                    ' TODO: FormID wird falsch oder nicht gesetzt
                    SelectedRecordID = RecordID
                    ' SelectedFormID = RowView.Item(1)
                    RefreshRecordByID(SelectedRecordID)

                    For Each control As Control In pnlView.Controls
                        'CONTROL_ID = GetControlID(thisrecordID, control)
                        CONTROL_ID = GetControlID_for_RecordID(control.Name, SelectedRecordID)
                        If TypeOf control Is TextBox Then
                            control.Text = String.Empty
                        ElseIf TypeOf control Is CheckBox Then
                            DirectCast(control, CheckBox).Checked = False
                        End If

                        If (CONTROL_ID <> -1) Then
                            Load_Value(SelectedRecordID, CONTROL_ID, control)
                            Refresh_CreatedChangedRecordByID(SelectedRecordID)
                        End If
                    Next
                End If
            End If

        Catch ex As Exception
            MsgBox("Error in FocusedRow_Change:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub


    'Private Sub VWPMO_CONTROL_SCREENBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
    '    Try

    '        Dim SQL As String
    '        Dim RECORD_ID As Integer
    '        Dim ADDED_WHO As String = Environment.UserName
    '        Dim CHANGED_WHO As String = Environment.UserName


    '        If insert = True Then
    '            ' Create the record first
    '            SQL = "INSERT INTO TBPMO_RECORD(ADDED_WHO) VALUES ('" & ADDED_WHO & "')"
    '            If ClassDatabase.Execute_non_Query(SQL) = False Then
    '                Exit Sub
    '            End If

    '            ' Get last RecordID 
    '            SQL = "SELECT (MAX(GUID)) FROM TBPMO_RECORD where ADDED_WHO = '" & ADDED_WHO & "'"
    '            RECORD_ID = ClassDatabase.Execute_Scalar(SQL)
    '            SelectedRecordID = RECORD_ID
    '            'MsgBox(formid)

    '            'Save Each Value from Controls into CONTROL_VALUE
    '            For Each ctrl As Control In pnlView.Controls
    '                Dim CONTROL_ID As Integer = GetControlID_for_RecordID(ctrl.Name, SelectedRecordID)

    '                If CONTROL_ID <> -1 And TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then

    '                    Dim VALUE = ctrl.Text

    '                    'If TypeOf ctrl Is ComboBox Then
    '                    '    Dim cmb = DirectCast(ctrl, ComboBox)
    '                    '    If cmb.DataSource Is Nothing = False Then
    '                    '        Dim bs As BindingSource = DirectCast(cmb.DataSource, BindingSource)
    '                    '        Dim dt As DataTable = DirectCast(bs.DataSource, DataTable)
    '                    '        BindingSource_Check_Content_Insert(bs, cmb.DisplayMember.ToString, cmb.Text)
    '                    '        '   MsgBox(dt.TableName.ToString & vbNewLine & cmb.DisplayMember.ToString)
    '                    '    End If
    '                    'End If

    '                    SQL = "INSERT INTO TBPMO_CONTROL_VALUE(CONTROL_ID, RECORD_ID, VALUE, ADDED_WHO) VALUES (" & CONTROL_ID & ", " & RECORD_ID & ", '" & VALUE & "', '" & ADDED_WHO & "')"
    '                    ClassDatabase.Execute_non_Query(SQL)

    '                End If
    '            Next

    '            ' Reset insert after INSERT
    '            insert = False
    '            ToolStrip_Notice("Neuer Datensatz eingefügt - " & Now)
    '            RefreshRecordByID(RECORD_ID)
    '        Else
    '            For Each ctrl As Control In pnlView.Controls
    '                'Dim CONTROL_ID = GetControlID(thisrecordID, ctrl)
    '                Dim CONTROL_ID = GetControlID_for_RecordID(ctrl.Name, SelectedRecordID)

    '                Dim value_akt = ClassDatabase.Execute_Scalar("SELECT VALUE FROM TBPMO_CONTROL_VALUE WHERE CONTROL_ID = " & CONTROL_ID & " AND RECORD_ID = " & SelectedRecordID)
    '                Console.WriteLine(CONTROL_ID.ToString & " - " & ctrl.Name)

    '                If CONTROL_ID = -1 And (TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox) Then
    '                    ' Trying to save a value for a new value
    '                    Dim VALUE = ctrl.Text
    '                    CONTROL_ID = GetControlID_for_Name(ctrl.Name, SelectedFormID)
    '                    SQL = "INSERT INTO TBPMO_CONTROL_VALUE (CONTROL_ID, RECORD_ID, VALUE, ADDED_WHO) VALUES (" & CONTROL_ID & ", " & SelectedRecordID & ", '" & VALUE & "', '" & ADDED_WHO & "')"
    '                    If ClassDatabase.Execute_non_Query(SQL) = False Then
    '                        Throw New Exception("Error while inserting new value for Control " & ctrl.Name & "!")
    '                    End If

    '                ElseIf TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then

    '                    Dim VALUE = ctrl.Text
    '                    If value_akt <> VALUE Then
    '                        'If TypeOf ctrl Is ComboBox Then
    '                        '    Dim cmb = DirectCast(ctrl, ComboBox)
    '                        '    If cmb.DataSource Is Nothing = False Then
    '                        '        Dim bs As BindingSource = DirectCast(cmb.DataSource, BindingSource)
    '                        '        BindingSource_Check_Content_Insert(bs, cmb.DisplayMember.ToString, cmb.Text)
    '                        '    End If
    '                        'End If

    '                        SQL = "UPDATE TBPMO_CONTROL_VALUE SET VALUE = '" & VALUE & "', CHANGED_WHO = '" & CHANGED_WHO & "' WHERE CONTROL_ID = " & CONTROL_ID & " AND RECORD_ID = " & SelectedRecordID
    '                        If ClassDatabase.Execute_non_Query(SQL) = False Then
    '                            Throw New Exception("Error while updating the value for Control " & ctrl.Name & "!")
    '                        End If
    '                    End If

    '                ElseIf CONTROL_ID = -1 And (TypeOf ctrl Is DevExpress.XtraEditors.DateEdit) Then
    '                    Dim date_ctrl As DevExpress.XtraEditors.DateEdit = DirectCast(ctrl, DevExpress.XtraEditors.DateEdit)
    '                    Dim VALUE As String = date_ctrl.DateTime.ToString("yyyy-MM-dd")
    '                    CONTROL_ID = GetControlID_for_Name(date_ctrl.Name, SelectedFormID)

    '                    SQL = "INSERT INTO TBPMO_CONTROL_VALUE (CONTROL_ID, RECORD_ID, VALUE, ADDED_WHO) VALUES (" & CONTROL_ID & ", " & SelectedRecordID & ", '" & VALUE & "', '" & ADDED_WHO & "')"
    '                    If ClassDatabase.Execute_non_Query(SQL) = False Then
    '                        Throw New Exception("Error while inserting new value for Control " & ctrl.Name & "!")
    '                    End If
    '                ElseIf TypeOf ctrl Is DevExpress.XtraEditors.DateEdit Then
    '                    Dim date_ctrl As DevExpress.XtraEditors.DateEdit = DirectCast(ctrl, DevExpress.XtraEditors.DateEdit)
    '                    Dim VALUE As String = date_ctrl.DateTime.ToString("yyyy-MM-dd")
    '                    If value_akt <> VALUE Then
    '                        SQL = "UPDATE TBPMO_CONTROL_VALUE SET VALUE = '" & VALUE & "', CHANGED_WHO = '" & CHANGED_WHO & "' WHERE CONTROL_ID = " & CONTROL_ID & " AND RECORD_ID = " & SelectedRecordID
    '                        If ClassDatabase.Execute_non_Query(SQL) = False Then
    '                            Throw New Exception("Error while updating the value for Control " & ctrl.Name & "!")
    '                        End If
    '                    End If
    '                    ss()
    '                End If

    '            Next
    '            RefreshRecordByID(SelectedRecordID)
    '            ToolStrip_Notice("Datensatz aktualisiert - " & Now)
    '        End If

    '        LoadOverview_Grid()

    '    Catch ex As Exception
    '        MsgBox("Error in VWPMO_CONTROL_SCREENBindingNavigatorSaveItem_Click:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
    '    End Try
    'End Sub
    'Sub BindingSource_Check_Content_Insert(bs As BindingSource, column As String, value As String)
    '    Try
    '        If bs.Contains(value) = False Then
    '            Dim dt As DataTable = DirectCast(bs.DataSource, DataTable)
    '            MsgBox(dt.TableName)
    '        End If



    '    Catch ex As Exception

    '    End Try
    'End Sub
    Private Sub ToolStrip_Notice(text As String)
        If text.Length > 0 Then
            tsstat_lbl.Text = text
            tsstat_lbl.Visible = True
        Else
            tsstat_lbl.Visible = False
        End If
    End Sub
    Private Sub RefreshRecordByID(RECORD_ID As Integer)
        Try
            Me.TBPMO_RECORDTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_RECORD, CType(RECORD_ID, Integer))
        Catch ex As System.Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs)
        Dim result As MsgBoxResult
        result = MessageBox.Show("Möchten Sie den Datensatz wirklich löschen?", "Bestätigung erforderlich:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = MsgBoxResult.Yes Then
            Dim sql As String = "DELETE FROM TBPMO_CONTROL_VALUE WHERE RECORD_ID = " & SelectedRecordID
            If ClassDatabase.Execute_non_Query(sql) = True Then ' Delete der Controls erfolgreich ausgeführt, jetzt der Record
                sql = "DELETE FROM TBPMO_RECORD WHERE GUID = " & SelectedRecordID
                If ClassDatabase.Execute_non_Query(sql) = True Then
                    LoadOverview_Grid()
                    ToolStrip_Notice("Der Datensatz wurde erfolgreich gelöscht - " & Now)
                End If
            End If

        End If
    End Sub

    Private Sub tsbtnrefresh_Click(sender As Object, e As EventArgs) Handles tsbtnrefresh.Click
        LoadOverview_Grid()
    End Sub

    Private Sub tsButtonAdd_Click(sender As Object, e As EventArgs) Handles tsButtonAdd.Click
        CtrlCommandUI.LoadControls(thisformid)
        'Das Flag INSERT nach LoadControls setzen
        CtrlCommandUI.IsInsert = True
    End Sub

    Private Sub tsButtonSave_Click(sender As Object, e As EventArgs) Handles tsButtonSave.Click
        Dim RecordID = gvMainView.GetFocusedRowCellValue(gvMainView.Columns("Record-ID"))
        If RecordID Is Nothing And gvMainView.RowCount > 0 Then
            Exit Sub
        Else
            If Not RecordID = Nothing Then
                SelectedRecordID = RecordID
            End If
        End If
        Dim ResultMessage = CtrlCommandUI.SaveRecord(SelectedRecordID, thisformid)

        LoadOverview_Grid()
        If SelectedRecordID > 0 Then
            Refresh_CreatedChangedRecordByID(SelectedRecordID)

        End If


        'Refresh_CreatedChangedRecordByID(thisrecordID)
        ToolStrip_Notice(ResultMessage)
        'tsstatus_Detail_show(True, ResultMessage)
    End Sub

    Private Sub tsButtonDelete_Click(sender As Object, e As EventArgs) Handles tsButtonDelete.Click
        Dim result As MsgBoxResult
        result = MessageBox.Show("Möchten Sie den Datensatz wirklich löschen?", "Bestätigung erforderlich:", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If result = MsgBoxResult.Yes Then
            If CtrlCommandUI.DeleteRecord(SelectedRecordID) = True Then
                LoadOverview_Grid()
                ToolStrip_Notice("Der Datensatz wurde erfolgreich gelöscht - " & Now)
            End If
        End If
    End Sub

    Sub tserstellt_Detail_show(vis As Boolean, txt As String)
        Me.tserstellt_Detail.Text = txt
        tserstellt_Detail.Visible = vis
    End Sub

    Public Sub Refresh_CreatedChangedRecordByID(RECORD_ID As Integer)
        Try
            Dim result = ClassDatabase.Execute_Scalar("select 'Added by ' + ADDED_WHO + ' when ' + CONVERT(VARCHAR(16),ADDED_WHEN,20) + ', Changed by ' + COALESCE(CHANGED_WHO,'') + ' when ' + COALESCE(CONVERT(VARCHAR(16),CHANGED_WHEN,20),'') FROM TBPMO_RECORD WHERE GUID = " & RECORD_ID)
            If IsDBNull(result) Or IsNothing(result) Then
                'Me.tserstellt_Details.Text = ""
                tserstellt_Detail_show(False, "")
            Else
                'Me.tserstellt_Details.Text = result.ToString
                tserstellt_Detail_show(True, result.ToString)
            End If

        Catch ex As System.Exception
            MsgBox("Error in Refresh_CreatedChangedRecordByID:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub gvMainView_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvMainView.FocusedRowChanged
        FocusedRow_Change()
    End Sub

    Private Sub StammdatenEditierenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StammdatenEditierenToolStripMenuItem.Click
        Dim MenuItem = DirectCast(sender, ToolStripMenuItem)
        Dim ContextMenu As ContextMenuStrip = MenuItem.Owner
        Dim SourceControl As Control = ContextMenu.SourceControl
        Dim MasterDataId As Integer = DirectCast(SourceControl.Tag, ClassControlMetadata).Id
        frmQuickInput.ShowDialog()
    End Sub
End Class

