Public Class frmLevel_Designer
#Region "+++++ Konstanten +++++"
    'Private akt_ctrl As Control
    Private Shared _Instance As frmLevel_Designer = Nothing
    Private Mouse_Moving As Boolean = False
    Private Mouse_Down As Boolean = False
    Private Current_Properties As Object = Nothing
    Private DesignerCtrlBuilder As ClassControlBuilder

    Private LastCursor As Point = Nothing
#End Region
    Dim formloaded As Boolean = False
    Public Shared Function Instance() As frmLevel_Designer
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmLevel_Designer
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Public Sub SetFormTitle(title As String)
        Me.Text = "Form Designer: " & title
    End Sub
    Public Sub SetFormName(name As String)
        Me.lblFormName.Text = "Name: " & name & " - Form-ID: " & CURRENT_ENTITY_ID
    End Sub
#Region "+++++ Drag Drop Funktionen +++++"

    Public Sub ControlDragDrop(type As String, Parent As GroupBox)
        Try
            Dim r As New Random()
            Dim def_font_family As String = DefaultFont.Name
            Dim def_font_color As Integer = DefaultForeColor.ToArgb
            Dim def_font_style As Integer = 0
            Dim def_font_size As Integer = 8

            Dim defaultCaption As String = "Bezeichner definieren"
            If USER_LANGUAGE <> "de-DE" Then
                defaultCaption = "Define Identifier"
            End If

            Dim defaultCalendarCaption As String = "Zu Kalender hinzufügen"
            If USER_LANGUAGE <> "de-DE" Then
                defaultCalendarCaption = "Add to Calendar"
            End If

            Dim random As String = ShortGUID()
            'Dim random As Integer = r.Next(8, 100)
            Select Case type
                Case "lbl"
                    DesignerCtrlBuilder.AddLabel(0, "lbl" & random, defaultCaption, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, "", True, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "line"
                    DesignerCtrlBuilder.AddLine(0, "line" & random, 0, 0, 0, 0, 0, True, Nothing, True)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "txt"
                    DesignerCtrlBuilder.AddTextBox(0, "txt" & random, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, True, 0, 0, False, False, False, "String", False, True, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "cmb"
                    DesignerCtrlBuilder.AddComboBox(0, "cmb" & random, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, True, 0, 0, "String", True, False, False, False, "", "", Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "dtp"
                    DesignerCtrlBuilder.AddDateTimePicker(0, "dtp" & random, 0, 0, def_font_family, def_font_size, def_font_style, 0, False, 0, 0, False, False, True, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "dgv"
                    DesignerCtrlBuilder.AddDataGridView(0, "dgv" & random, 0, 0, 0, 0, True, False, False, "dgv" & random, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "chk"
                    DesignerCtrlBuilder.AddCheckBox(0, "chk" & random, defaultCaption, False, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, False, 0, 0, True, False, False, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "rb"
                    DesignerCtrlBuilder.AddRadioButton(0, "rb" & random, defaultCaption, False, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, False, 0, 0, True, False, False, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "pb"
                    DesignerCtrlBuilder.AddPictureBox(0, "pb" & random, 0, 0, 0, 0, True, False, False, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "gb"
                    DesignerCtrlBuilder.AddGroupBox(0, "gb" & random, defaultCaption, 0, 0, 0, 0, ColorToInt(SystemColors.Control), def_font_color, def_font_family, def_font_size, def_font_style, True, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "f_addappointment"
                    DesignerCtrlBuilder.FunctionAddAppointment(0, "f_addappointment" & random, defaultCalendarCaption, 0, 0, 0, 0, True, Parent)

                    If ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl) = True Then
                        Dim ControlId = GetControlID_for_Name(DesignerCtrlBuilder.CurrentControl.Name, CURRENT_ENTITY_ID)
                        ClassFunctionCommands.InsertFunction(ControlId, "ADDAPPOINTMENT")
                    End If
                Case "f_addformdata"
                    DesignerCtrlBuilder.FunctionAddFormData(0, "f_addformdata" & random, "Add Form Data", 0, 0, 0, 0, True, Parent)

                    If ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl) = True Then
                        Dim ControlId = GetControlID_for_Name(DesignerCtrlBuilder.CurrentControl.Name, CURRENT_ENTITY_ID)
                        ClassFunctionCommands.InsertFunction(ControlId, "ADDFORMDATA")
                    End If
                Case "lstbxcheck"
                    DesignerCtrlBuilder.AddCheckedListBox(0, "chlb" & random, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, True, 0, 0, True, False, False, "", "", Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "lstbx"
                    DesignerCtrlBuilder.AddListBox(0, "lbx" & random, 0, 0, def_font_family, def_font_color, def_font_size, def_font_style, 0, True, 0, 0, True, False, False, "", "", Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case "dgvcheck"
                    DesignerCtrlBuilder.AddDataGridViewCheckable(0, "dgvcheck" & random, 0, 0, 0, 0, True, 0, False, False, False, "dgvcheck" & random, Parent)
                    ' Aktuelles Control in die Datenbank speichern
                    ClassControlCommands.InsertControl(DesignerCtrlBuilder.CurrentControl)
                Case Else
                    MsgBox("Event '" & type & "' nicht definiert")
            End Select
        Catch ex As Exception
            MsgBox("unexpected Error in ControlDragDrop: " & vbNewLine, MsgBoxStyle.Critical)
        End Try

    End Sub

    Public Sub Groupbox_DragDrop(sender As Object, e As DragEventArgs)
        Dim type As String = e.Data.GetData(DataFormats.Text)
        Dim GroupBox As GroupBox = CType(sender, GroupBox)

        ControlDragDrop(type, GroupBox)
    End Sub

    Private Sub pnlDesigner_DragDrop(sender As Object, e As DragEventArgs) Handles pnlDesigner.DragDrop
        Dim type As String = e.Data.GetData(DataFormats.Text)

        ControlDragDrop(type, Nothing)
    End Sub

    Private Sub pnlDesigner_DragEnter(sender As Object, e As DragEventArgs) Handles pnlDesigner.DragEnter
        ' Check the format of the data being dropped. 
        If (e.Data.GetDataPresent(DataFormats.Text)) Then
            ' Display the copy cursor. 
            e.Effect = DragDropEffects.Copy
        Else
            ' Display the no-drop cursor. 
            e.Effect = DragDropEffects.None
        End If
    End Sub

#End Region

#Region "+++++ Form Events +++++"
    Private Sub frmLevel_Designer_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Try
            Dim sw

            sw = Stopwatch.StartNew()
            ClassWindowLocation.SaveFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmLevel_Designer")
            sw.Stop()
            Console.WriteLine(String.Format("SaveFormLocationSize took {0}ms", sw.ElapsedMilliseconds))

            sw = Stopwatch.StartNew()
            My.Settings.Save()
            sw.Stop()
            Console.WriteLine(String.Format("My.Settings.Save took {0}ms", sw.ElapsedMilliseconds))

            sw = Stopwatch.StartNew()
            RebuildView()
            sw.Stop()
            Console.WriteLine(String.Format("RebuildView took {0}ms", sw.ElapsedMilliseconds))

            Dim wid As Integer = Me.Width
            Dim update_sizepnl As String = "UPDATE TBPMO_FORM_VIEW SET HEIGHT = " & pnlDesigner.Height & ",WIDTH = " & wid & " where FORM_ID = " & CURRENT_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID
            MYDB_ECM.ExecuteNonQuery(update_sizepnl)
        Catch ex As Exception
            MsgBox("Error in frmLevel_Designer_FormClosing: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub frmLevel_Designer_Load(sender As Object, e As EventArgs) Handles Me.Load
        ClassWindowLocation.LoadFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmLevel_Designer")

        DesignerCtrlBuilder = New ClassControlBuilder(pnlDesigner,
                                     AddressOf Control_MouseDown,
                                     AddressOf Control_MouseUp,
                                     AddressOf Control_MouseMove,
                                     AddressOf Control_Click,
                                     AddressOf Groupbox_DragDrop)

        LoadFormProperties()
        LoadControls()

    End Sub

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

#Region "+++++ Load Funktionen +++++"
    Public Sub LoadFormProperties()
        Dim NAME_SQL As String = "SELECT NAME FROM TBPMO_FORM WHERE GUID = " & CURRENT_ENTITY_ID
        Dim formviewid = MYDB_ECM.GetScalarValue("SELECT GUID FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & CURRENT_ENTITY_ID & " AND SCREEN_ID = " & CURRENT_SCREEN_ID)
        Dim TITLE_SQL As String = "SELECT CAPTION FROM TBPMO_LANGUAGE_OBJECT WHERE PMO_OBJECT_NAME = 'FORMVIEW_TITLE" & formviewid & "' and SCREEN_ID = " & CURRENT_SCREEN_ID & " AND LANGUAGE_TYPE = '" & USER_LANGUAGE & "'"
        Dim Height = MYDB_ECM.GetScalarValue("SELECT HEIGHT FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & CURRENT_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)
        Dim width = MYDB_ECM.GetScalarValue("SELECT WIDTH FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & CURRENT_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)
        Dim name = MYDB_ECM.GetScalarValue(NAME_SQL)
        Dim title = MYDB_ECM.GetScalarValue(TITLE_SQL)
        '  Check_Document_Config(CURRENT_ENTITY_ID)
        If Not IsDBNull(Height) Then
            If Height > 100 Then
                Me.pnlDesigner.Height = Height
            End If
            'If Not IsDBNull(width) Then
            If width > 500 Then
                ' Me.Width = width
            End If

            'End If
        End If
        SetFormName(name)
        SetFormTitle(title)
    End Sub

    Public Sub LoadControls()
        Dim sql = String.Format("SELECT T.*, dbo.FNPMO_GET_CONTROL_CAPTION ('{0}', {1},T.CONTROL_ID) AS 'CAPTION' FROM VWPMO_CONTROL_SCREEN T" & _
                                " where T.FORM_ID = {2} and T.SCREEN_ID = {3}", USER_LANGUAGE, CURRENT_SCREEN_ID, CURRENT_ENTITY_ID, CURRENT_SCREEN_ID)
        Dim dt As DataTable = MYDB_ECM.GetDatatable(sql)

        For Each dr As DataRow In dt.Rows

            Dim parent As GroupBox = Nothing
            If (dr.Item("CONTROL_PARENT_ID") <> 0) Then
                Dim parentname As String = Get_Name_for_ControlID(dr.Item("CONTROL_PARENT_ID"), CURRENT_ENTITY_ID)
                parent = DesignerCtrlBuilder.GetControlByName(parentname)
            End If

            Select Case dr.Item("CTRLTYPE_ID")
                Case 1 ' Label
                    DesignerCtrlBuilder.AddLabel(dr.Item("CONTROL_ID"),
                                         dr.Item("CONTROL_NAME"),
                                         dr.Item("CAPTION"),
                                         dr.Item("CTRLSCR_X_LOC"),
                                         dr.Item("CTRLSCR_Y_LOC"),
                                         dr.Item("CTRLSCR_FONT_FAMILY"),
                                         dr.Item("CTRLSCR_FONT_COLOR"),
                                         dr.Item("CTRLSCR_FONT_SIZE"),
                                         dr.Item("CTRLSCR_FONT_STYLE"),
                                         dr.Item("CONTROL_SQLCOMMAND_1"),
                                         False,
                                         parent,
                                         True)
                Case 2 ' TextBox
                    DesignerCtrlBuilder.AddTextBox(dr.Item("CONTROL_ID"),
                                           dr.Item("CONTROL_NAME"),
                                           dr.Item("CTRLSCR_X_LOC"),
                                           dr.Item("CTRLSCR_Y_LOC"),
                                           dr.Item("CTRLSCR_FONT_FAMILY"),
                                           dr.Item("CTRLSCR_FONT_COLOR"),
                                           dr.Item("CTRLSCR_FONT_SIZE"),
                                           dr.Item("CTRLSCR_FONT_STYLE"),
                                           dr.Item("CTRLSCR_TAB_INDEX"),
                                           dr.Item("CTRLSCR_TAB_STOP"),
                                           dr.Item("CTRLSCR_WIDTH"),
                                           dr.Item("CTRLSCR_HEIGHT"),
                                           dr.Item("CONTROL_MULTILINE"),
                                           dr.Item("CONTROL_READ_ONLY"),
                                           dr.Item("CONTROL_REQUIRED"),
                                           dr.Item("CONTROL_FORMAT_TYPE"),
                                           dr.Item("CONTROL_AUTO_SUGGEST"),
                                           False,
                                           parent,
                                         True)
                Case 3 ' ComboBox
                    DesignerCtrlBuilder.AddComboBox(dr.Item("CONTROL_ID"),
                                            dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            dr.Item("CONTROL_FORMAT_TYPE"),
                                            False,
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_SELECT_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            dr.Item("CONTROL_STATIC_LIST"),
                                            dr.Item("CONTROL_SQLCOMMAND_1"),
                                            parent,
                                            True)
                Case 4 ' DateTimePicker
                    DesignerCtrlBuilder.AddDateTimePicker(dr.Item("CONTROL_ID"),
                                                  dr.Item("CONTROL_NAME"),
                                                  dr.Item("CTRLSCR_X_LOC"),
                                                  dr.Item("CTRLSCR_Y_LOC"),
                                                  dr.Item("CTRLSCR_FONT_FAMILY"),
                                                  dr.Item("CTRLSCR_FONT_SIZE"),
                                                  dr.Item("CTRLSCR_FONT_STYLE"),
                                                  dr.Item("CTRLSCR_TAB_INDEX"),
                                                  dr.Item("CTRLSCR_TAB_STOP"),
                                                  dr.Item("CTRLSCR_WIDTH"),
                                                  dr.Item("CTRLSCR_HEIGHT"),
                                                  dr.Item("CONTROL_READ_ONLY"),
                                                  dr.Item("CONTROL_REQUIRED"),
                                                  False,
                                                  parent,
                                                  True)
                Case 5 ' GroupBox
                    DesignerCtrlBuilder.AddGroupBox(dr.Item("CONTROL_ID"),
                                            dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_CAPTION"),
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            dr.Item("CTRLSCR_BACK_COLOR"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            False,
                                            parent,
                                            True)
                Case 6 ' PictureBox
                    DesignerCtrlBuilder.AddPictureBox(dr.Item("CONTROL_ID"),
                                              dr.Item("CONTROL_NAME"),
                                              dr.Item("CTRLSCR_X_LOC"),
                                              dr.Item("CTRLSCR_Y_LOC"),
                                              dr.Item("CTRLSCR_WIDTH"),
                                              dr.Item("CTRLSCR_HEIGHT"),
                                              False,
                                              dr.Item("CONTROL_READ_ONLY"),
                                              dr.Item("CONTROL_REQUIRED"),
                                              parent,
                                              True)
                Case 7 ' DataGridView
                    DesignerCtrlBuilder.AddDataGridView(dr.Item("CONTROL_ID"),
                                                dr.Item("CONTROL_NAME"),
                                                dr.Item("CTRLSCR_X_LOC"),
                                                dr.Item("CTRLSCR_Y_LOC"),
                                                dr.Item("CTRLSCR_WIDTH"),
                                                dr.Item("CTRLSCR_HEIGHT"),
                                                False,
                                                dr.Item("CONTROL_READ_ONLY"),
                                                dr.Item("CONTROL_REQUIRED"),
                                                dr.Item("CONTROL_COL_NAME"),
                                                parent,
                                                True)
                Case 10 ' Checkbox
                    Dim Checked As Boolean
                    If IsDBNull(dr.Item("CONTROL_DEF_VALUE")) Then
                        Checked = False
                    Else
                        Checked = StrToBool(dr.Item("CONTROL_DEF_VALUE"))
                    End If
                    DesignerCtrlBuilder.AddCheckBox(dr.Item("CONTROL_ID"),
                                            dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_CAPTION"),
                                            Checked,
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            False,
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            parent,
                                            True)
                Case 8 'AddAppointment Button
                    DesignerCtrlBuilder.FunctionAddAppointment(dr.Item("CONTROL_ID"),
                                                       dr.Item("CONTROL_NAME"),
                                                       dr.Item("CTRLSCR_CAPTION"),
                                                       dr.Item("CTRLSCR_X_LOC"),
                                                       dr.Item("CTRLSCR_Y_LOC"),
                                                       dr.Item("CTRLSCR_WIDTH"),
                                                       dr.Item("CTRLSCR_HEIGHT"),
                                                       False,
                                                       parent,
                                                       True)
                Case 9 'AddFormData
                    DesignerCtrlBuilder.FunctionAddFormData(dr.Item("CONTROL_ID"),
                                                    dr.Item("CONTROL_NAME"),
                                                       dr.Item("CTRLSCR_CAPTION"),
                                                       dr.Item("CTRLSCR_X_LOC"),
                                                       dr.Item("CTRLSCR_Y_LOC"),
                                                       dr.Item("CTRLSCR_WIDTH"),
                                                       dr.Item("CTRLSCR_HEIGHT"),
                                                       False,
                                                       parent,
                                                       True)

                Case 11 ' RadioButton
                    Dim Checked As Boolean
                    If IsDBNull(dr.Item("CONTROL_DEF_VALUE")) Then
                        Checked = False
                    Else
                        Checked = StrToBool(dr.Item("CONTROL_DEF_VALUE"))
                    End If
                    DesignerCtrlBuilder.AddRadioButton(dr.Item("CONTROL_ID"),
                                               dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_CAPTION"),
                                            Checked,
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            False,
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            parent,
                                            True)
                Case 12 'CheckedListBox
                    DesignerCtrlBuilder.AddCheckedListBox(dr.Item("CONTROL_ID"),
                                                  dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            False,
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            dr.Item("CONTROL_STATIC_LIST"),
                                            dr.Item("CONTROL_SQLCOMMAND_1"),
                                            parent,
                                            True)
                Case 13 'ListBox
                    DesignerCtrlBuilder.AddListBox(dr.Item("CONTROL_ID"),
                                           dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_FONT_FAMILY"),
                                            dr.Item("CTRLSCR_FONT_COLOR"),
                                            dr.Item("CTRLSCR_FONT_SIZE"),
                                            dr.Item("CTRLSCR_FONT_STYLE"),
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            False,
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            dr.Item("CONTROL_STATIC_LIST"),
                                            dr.Item("CONTROL_SQLCOMMAND_1"),
                                            parent,
                                            True)
                Case 14 'DataGridViewCheckable
                    DesignerCtrlBuilder.AddDataGridViewCheckable(
                                            dr.Item("CONTROL_ID"),
                                            dr.Item("CONTROL_NAME"),
                                            dr.Item("CTRLSCR_X_LOC"),
                                            dr.Item("CTRLSCR_Y_LOC"),
                                            dr.Item("CTRLSCR_WIDTH"),
                                            dr.Item("CTRLSCR_HEIGHT"),
                                            False,
                                            dr.Item("CTRLSCR_TAB_INDEX"),
                                            dr.Item("CTRLSCR_TAB_STOP"),
                                            dr.Item("CONTROL_READ_ONLY"),
                                            dr.Item("CONTROL_REQUIRED"),
                                            dr.Item("CONTROL_COL_NAME"),
                                            parent,
                                            True)
                Case 15 ' Line
                    DesignerCtrlBuilder.AddLine(
                        dr.Item("CONTROL_ID"),
                        dr.Item("CONTROL_NAME"),
                        dr.Item("CTRLSCR_X_LOC"),
                        dr.Item("CTRLSCR_Y_LOC"),
                        dr.Item("CTRLSCR_FONT_COLOR"),
                        dr.Item("CTRLSCR_WIDTH"),
                        dr.Item("CTRLSCR_HEIGHT"),
                        False,
                        parent,
                        True
                    )
            End Select
        Next
    End Sub


#End Region

#Region "+++++ Mouse Events +++++"
    Private Sub Control_Click(sender As Object, e As EventArgs)
        Dim control As Control = DirectCast(sender, Control)

        ' Aktuelles Control highlighten
        control.BringToFront()
        DesignerCtrlBuilder.SetAllActiveColor()

        ' GUID des aktuellen Controls speichern 
        CURRENT_CONTROL_ID = ClassControlCommands.GetControlGuid(DesignerCtrlBuilder.CurrentControl.Name)

        ' Control Eigenschaften laden
        pgControlProperties.Enabled = True

        LoadControlPropertiesNeu(DesignerCtrlBuilder.CurrentControl)
    End Sub


    Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        ' Check to see if the correct button has been pressed
        If e.Button = System.Windows.Forms.MouseButtons.Left Then
            DesignerCtrlBuilder.CurrentControl = sender
            DesignerCtrlBuilder.BeginLocation = e.Location

            ' Set the mode flag to signal the MouseMove event handler that it
            ' needs to now calculate new positions for our control
            Mouse_Moving = True
        End If
    End Sub

    Private Sub ControlButton_MouseDown(sender As Object, e As MouseEventArgs) Handles btntextbox.MouseDown,
                                                                                btnlabel.MouseDown,
                                                                                btndtp.MouseDown,
                                                                                btncmb.MouseDown,
                                                                                btnCheckbox.MouseDown,
                                                                                btnpb.MouseDown,
                                                                                btngb.MouseDown,
                                                                                btn_addtocal.MouseDown,
                                                                                btn_addformdata.MouseDown,
                                                                                btnRadioButton.MouseDown,
                                                                                btnListBoxCheckable.MouseDown,
                                                                                btnListBox.MouseDown,
                                                                                btnTableCheckable.MouseDown, btnLine.MouseDown
        Mouse_Down = True
    End Sub

    Private Sub ControlButton_MouseMove(sender As Object, e As MouseEventArgs) Handles btntextbox.MouseMove,
                                                                            btnlabel.MouseMove,
                                                                            btndtp.MouseMove,
                                                                            btncmb.MouseMove,
                                                                            btnCheckbox.MouseMove,
                                                                            btnpb.MouseMove,
                                                                            btngb.MouseMove,
                                                                            btn_addtocal.MouseMove,
                                                                            btn_addformdata.MouseMove,
                                                                            btnRadioButton.MouseMove,
                                                                            btnListBoxCheckable.MouseMove,
                                                                            btnListBox.MouseMove,
                                                                            btnTableCheckable.MouseMove, btnLine.MouseMove

        If Mouse_Down Then
            Dim ctrl As Button = DirectCast(sender, Button)
            Dim type As String = ctrl.Tag.ToString()

            ctrl.DoDragDrop(type, DragDropEffects.Copy)
        End If
        Mouse_Down = False
    End Sub

    Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
        ' The button was released, so we're going back to Static mode.
        If Mouse_Moving = True Then
            Mouse_Moving = False
            'end_location = e.Location
            DesignerCtrlBuilder.EndLocation = e.Location
            Dim currentPosition As Point = DesignerCtrlBuilder.CurrentControl.Location

            ' Das Control sollte nicht außerhalb des Panels geschoben werden (Koordinaten kleiner 0)
            If currentPosition.X < 0 Then
                DesignerCtrlBuilder.CurrentControl.Location = New Point(0, DesignerCtrlBuilder.CurrentControl.Location.Y)
            End If

            If currentPosition.Y < 0 Then
                DesignerCtrlBuilder.CurrentControl.Location = New Point(DesignerCtrlBuilder.CurrentControl.Location.X, 0)
            End If

            ' Ebenso nicht über die Größe des Panels (X-Achse)
            If currentPosition.X > pnlDesigner.Width Then
                DesignerCtrlBuilder.CurrentControl.Location = New Point(pnlDesigner.Width - DesignerCtrlBuilder.CurrentControl.Width, DesignerCtrlBuilder.CurrentControl.Location.Y)
            End If

            ' Ebenso nicht über die Größe des Panels (Y-Achse)
            If currentPosition.Y > pnlDesigner.Height Then
                DesignerCtrlBuilder.CurrentControl.Location = New Point(DesignerCtrlBuilder.CurrentControl.Location.X, pnlDesigner.Height - DesignerCtrlBuilder.CurrentControl.Height)
            End If

            'frmTool_ControlProperties.Instance.UpdateControlLocation(CtrlBuilder.CurrentControl)
            UpdateControlLocation(DesignerCtrlBuilder.CurrentControl)
            ClassControlCommands.UpdateControlPosition(DesignerCtrlBuilder.CurrentControl.Location.X,
                                                       DesignerCtrlBuilder.CurrentControl.Location.Y,
                                                       DesignerCtrlBuilder.CurrentControl)
            TabControl1.SelectTab("pageProperties")

            'Save_Control()
        End If
        ' Restore the cursor image to the way we found it when the mouse
        ' button was pressed
        MyBase.Cursor = Cursors.Default
    End Sub
    Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Try
            If DesignerCtrlBuilder.CurrentControl Is Nothing Then
                Exit Sub
            End If

            If Mouse_Moving = True Then
                Me.Cursor = Cursors.Hand
                Me.Refresh()

                Dim control As Control = DirectCast(sender, Control)
                Dim NowCursor As Point = DesignerCtrlBuilder.GetCursorPosition()

                ' Wenn das Control in einer GroupBox steckt
                If ParentIsGroupBox(control) Then
                    Dim p As Control = control.Parent
                    Dim newPos As Point = New Point(NowCursor.X - p.Location.X - e.Location.X,
                                                    NowCursor.Y - p.Location.Y - e.Location.Y)

                    ' Verhindert, dass das Control links+oben außerhalb der Groupbox geschoben wird = unerreichbar
                    If newPos.X < 0 Or newPos.Y < 0 Then
                        Exit Sub
                    End If

                    ' Verhindert, dass das Control rechts+unten außerhalb der Groupbox geschoben wird = unerreichbar
                    If (newPos.X + control.Size.Width) > p.Size.Width Or (newPos.Y + control.Size.Height) > p.Size.Height Then
                        Exit Sub
                    End If

                    If Point.op_Inequality(NowCursor, LastCursor) Then
                        control.Location = newPos
                        LastCursor = NowCursor
                    End If

                Else
                    ' Alle normalen Controls
                    control.Location = New Point(NowCursor.X - DesignerCtrlBuilder.BeginLocation.X,
                                                 NowCursor.Y - DesignerCtrlBuilder.BeginLocation.Y)
                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "MovableLabel_MouseMove")
            Mouse_Moving = False
        End Try

    End Sub
#End Region

    Private Sub Control_PressTab(sender As Object, e As PreviewKeyDownEventArgs)
        'NOOP
    End Sub

    Private Function RebuildView()
        Try
            'Dim sql = "EXECUTE PRPMO_CREATE_SQL " & CURRENT_ENTITY_ID
            'ClassDatabase.Execute_non_Query_Async(sql)
            'Dim sql2 = "EXECUTE PRPMO_CREATE_TEMP_VIEWS"
            'ClassDatabase.Execute_non_Query_Async(sql2)
            'Return True

            Dim noerror As Boolean = False
            Dim sql = String.Format("DECLARE @return_value int " & vbNewLine &
                   "EXEC @return_value = dbo.PRPMO_CREATE_ENTITY_TABLE {0}" & vbNewLine &
                   "IF @return_value = 0 EXEC @return_value = [dbo].[PRPMO_CREATE_ENTITY_TABLE_VIEW] {0}" & vbNewLine &
                   "SELECT	'Return Value' = @return_value", CURRENT_ENTITY_ID)
            noerror = MYDB_ECM.ExecuteNonQuery(sql)
            If noerror = False Then
                MsgBox("Error while rebuilding view! See Log for more information", MsgBoxStyle.Critical)
            End If
            Return noerror
        Catch ex As Exception
            MsgBox("Error in btnexecute_CreateViewSql:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
            Return False
        End Try
    End Function


    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click

        If RebuildView() = True Then
            MsgBox("The Views have been created successful.", MsgBoxStyle.Information)
        Else
            MsgBox("Error while creating views!", MsgBoxStyle.Critical)
        End If

    End Sub

    Private Sub frmLevel_Designer_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        formloaded = True
    End Sub

    Public Sub LoadControlPropertiesNeu(ctrl As Control)
        Try

            Dim controlId = DirectCast(ctrl.Tag, ClassControlMetadata).Id
            Dim sql As String = String.Format("SELECT T.*, dbo.FNPMO_GET_CONTROL_CAPTION ('{0}',{1},{2}) AS 'CAPTION_TRANSLATED' FROM VWPMO_CONTROL_SCREEN T WHERE CONTROL_ID = {2}", USER_LANGUAGE, CURRENT_SCREEN_ID, controlId)
            'Dim sql As String = String.Format("SELECT * FROM VWPMO_CONTROL_SCREEN WHERE CONTROL_ID = {0}", controlId)
            'Dim sqlHint As String = String.Format("SELECT HINT FROM TBPMO_CONTROL_LANGUAGE WHERE CONTROL_SCREEN_ID = {0} AND LANGUAGE_TYPE = '{1}'", controlId, USER_LANGUAGE)
            Dim sqlHint = String.Format("SELECT HINT FROM TBPMO_CONTROL_LANGUAGE WHERE CONTROL_SCREEN_ID = (SELECT GUID FROM TBPMO_CONTROL_SCREEN WHERE CONTROL_ID = {0} AND SCREEN_ID = {1}) AND LANGUAGE_TYPE = '{2}'", controlId, CURRENT_SCREEN_ID, USER_LANGUAGE)

            Dim dt As DataTable = MYDB_ECM.GetDatatable(sql)
            Dim hint As String = ClassConverter.ToStringOrDefault(MYDB_ECM.GetScalarValue(sqlHint), Nothing)

            Dim props As Object = Nothing
            Dim r As DataRow = Nothing
            Dim type As String = Nothing

            If dt.Rows.Count <> 1 Then
                MsgBox("Unexpected behaviour in LoadControlProperties: No Control could be loaded!" & vbNewLine & "Tag.ControlID: " & controlId.ToString, MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            r = dt.Rows(0)
            type = r.Item("CTRLTYPE_NAME")

            'Props Object initialisieren
            Select Case type
                Case "Label" : props = New LabelProperties()
                Case "Textbox" : props = New TextBoxProperties()
                Case "Combobox" : props = New ComboBoxProperties()
                Case "Checkbox" : props = New CheckBoxProperties()
                Case "RadioButton" : props = New RadioButtonProperties()
                Case "Datepicker" : props = New DateTimePickerProperties()
                Case "Datagridview" : props = New DataGridViewProperties()
                Case "Groupbox" : props = New GroupBoxProperties()
                Case "Picturebox" : props = New PictureBoxProperties()
                Case "ListBox" : props = New ComboBoxProperties()
                Case "CheckedListBox" : props = New ComboBoxProperties()
                Case "DatagridviewCheckable" : props = New ComboBoxProperties()
                Case "F_AddAppointment" : props = New FunctionAddAppointmentProperties()
                Case "F_AddFormData" : props = New FunctionAddFormDataProperties()
                Case "Line" : props = New LineProperties()
            End Select

            'Generische Properties laden
            props.ID = r.Item("CONTROL_ID")
            props.Name = r.Item("CONTROL_NAME")
            props.Size = New Size(
                r.Item("CTRLSCR_WIDTH"),
                r.Item("CTRLSCR_HEIGHT")
            )
            props.Location = New Point(
                r.Item("CTRLSCR_X_LOC"),
                r.Item("CTRLSCR_Y_LOC")
            )
            props.Visible = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_VISIBLE"), True)
            props.Hint = hint

            'Control-spezifische Properties laden
            Select Case type
                Case "Label"
                    props.ControlType = "Label"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")

                    props.Caption = r.Item("CAPTION_TRANSLATED")
                    'props.Caption = r.Item("CTRLSCR_CAPTION")
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))


                Case "Line"
                    props.ControlType = "Line"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "Textbox"
                    props.ControlType = "TextBox"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")
                    props.DefaultValue = ClassConverter.ToStringOrDefault(r.Item("CONTROL_DEF_VALUE"))
                    props.Format = NotNull([Enum].Parse(GetType(EnumFormatOptions), r.Item("CONTROL_FORMAT_TYPE")), EnumFormatOptions.String)
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.TabStop = r.Item("CTRLSCR_TAB_STOP")
                    props.TabIndex = r.Item("CTRLSCR_TAB_INDEX")
                    props.ShowColumn = NotNull(CBool(r.Item("CONTROL_SHOW_COLUMN")), True)
                    props.Multiline = r.Item("CONTROL_MULTILINE")
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))
                    props.AutoSuggest = CBool(r.Item("CONTROL_AUTO_SUGGEST"))

                Case "Combobox"
                    props.ControlType = "ComboBox"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")
                    props.DefaultValue = ClassConverter.ToStringOrDefault(r.Item("CONTROL_DEF_VALUE"))
                    props.MasterDataId = r.Item("CTRLSCR_MASTER_DATA_ID")
                    props.Format = NotNull([Enum].Parse(GetType(EnumFormatOptions), r.Item("CONTROL_FORMAT_TYPE")), EnumFormatOptions.String)
                    'props.StaticList = NotNull(r.Item("CONTROL_STATIC_LIST"), "")
                    props.StaticList = New StaticListValue(r.Item("CONTROL_STATIC_LIST").ToString())
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.IsSelectOnly = r.Item("CONTROL_SELECT_ONLY")
                    props.TabStop = r.Item("CTRLSCR_TAB_STOP")
                    props.TabIndex = r.Item("CTRLSCR_TAB_INDEX")
                    props.ShowColumn = NotNull(CBool(r.Item("CONTROL_SHOW_COLUMN")), True)
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "Checkbox"
                    props.ControlType = "CheckBox"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")
                    props.Caption = r.Item("CTRLSCR_CAPTION")
                    props.DefaultValue = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_DEF_VALUE"))
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.ShowColumn = NotNull(CBool(r.Item("CONTROL_SHOW_COLUMN")), True)
                    props.TabStop = r.Item("CTRLSCR_TAB_STOP")
                    props.TabIndex = r.Item("CTRLSCR_TAB_INDEX")
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())

                Case "RadioButton"
                    props.ControlType = "RadioButton"
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")
                    props.Caption = r.Item("CTRLSCR_CAPTION")
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())

                Case "Datepicker"
                    props.ControlType = "DatePicker"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = r.Item("CONTROL_COL_NAME")
                    Dim i = r.Item("CONTROL_DEF_VALUE")
                    props.DefaultValue = NotNull(Of Object)(r.Item("CONTROL_DEF_VALUE"), EnumDateTimePickerDefaultValueOptions.Empty)
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.ShowColumn = NotNull(CBool(r.Item("CONTROL_SHOW_COLUMN")), True)
                    props.TabStop = r.Item("CTRLSCR_TAB_STOP")
                    props.TabIndex = r.Item("CTRLSCR_TAB_INDEX")
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "Datagridview"
                    props.ControlType = "DataGridView/Table"
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = ClassConverter.ToStringOrDefault(r.Item("CONTROL_COL_NAME"), ctrl.Name)
                    props.ShowColumn = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_SHOW_COLUMN"), True)
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "Groupbox"
                    props.ControlType = "GroupBox"
                    props.Caption = r.Item("CTRLSCR_CAPTION")
                    props.BackColor = IntToColor(r.Item("CTRLSCR_BACK_COLOR"))

                Case "Picturebox"
                    props.ControlType = "PictureBox"
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "ListBox"
                    props.ControlType = "ListBox"
                    'props.StaticList = ClassConverter.ToStringOrDefault(r.Item("CONTROL_STATIC_LIST"))
                    props.StaticList = New StaticListValue(r.Item("CONTROL_STATIC_LIST").ToString())
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = ClassConverter.ToStringOrDefault(r.Item("CONTROL_COL_NAME"), ctrl.Name)
                    props.ShowColumn = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_SHOW_COLUMN"), True)
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "CheckedListBox"
                    props.ControlType = "CheckedListBox"
                    'props.StaticList = ClassConverter.ToStringOrDefault(r.Item("CONTROL_STATIC_LIST"))
                    props.StaticList = New StaticListValue(r.Item("CONTROL_STATIC_LIST").ToString())
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = ClassConverter.ToStringOrDefault(r.Item("CONTROL_COL_NAME"), ctrl.Name)
                    props.ShowColumn = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_SHOW_COLUMN"), True)
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "DatagridviewCheckable"
                    props.ControlType = "DataGridView/Table Checkable"
                    props.StaticList = New StaticListValue(r.Item("CONTROL_STATIC_LIST").ToString())
                    props.FontColor = IntToColor(r.Item("CTRLSCR_FONT_COLOR"))
                    props.Font = New Font(r.Item("CTRLSCR_FONT_FAMILY").ToString(),
                                          CType(r.Item("CTRLSCR_FONT_SIZE"), Single),
                                          CType(r.Item("CTRLSCR_FONT_STYLE"), FontStyle))
                    props.ColumnTitle = ClassConverter.ToStringOrDefault(r.Item("CONTROL_COL_NAME"), ctrl.Name)
                    props.ShowColumn = ClassConverter.ToBooleanOrDefault(r.Item("CONTROL_SHOW_COLUMN"), True)
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))
                    props.EnabledWhen = New SQLValue(r.Item("CONTROL_SQLCOMMAND_2").ToString())
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")
                    props.TabStop = r.Item("CTRLSCR_TAB_STOP")
                    props.TabIndex = r.Item("CTRLSCR_TAB_INDEX")
                    props.TreeView = CBool(r.Item("CONTROL_TREE_VIEW"))

                Case "F_AddAppointment"
                    props.ControlType = "Function.AddToCalendar"
                    props.Caption() = r.Item("CTRLSCR_CAPTION")

                    Dim f = ClassFunctionCommands.LoadFunction(controlId)
                    Dim FromDateId, ToDateId, SubjectString, Subject2String, PlaceString, DescString

                    FromDateId = f.Item("INTEGER1")
                    If IsDBNull(FromDateId) OrElse FromDateId = 0 Then
                        props.FromDate = ""
                    Else
                        props.FromDate = Get_Name_for_ControlID(FromDateId, CURRENT_ENTITY_ID)
                    End If

                    ToDateId = f.Item("INTEGER2")
                    If IsDBNull(ToDateId) OrElse ToDateId = 0 Then
                        props.ToDate = ""
                    Else
                        props.ToDate = Get_Name_for_ControlID(ToDateId, CURRENT_ENTITY_ID)
                    End If

                    SubjectString = f.Item("STRING1")
                    If IsDBNull(SubjectString) OrElse String.IsNullOrEmpty(SubjectString) Then
                        props.Subject = ""
                    Else
                        props.Subject = SubjectString
                    End If

                    Subject2String = f.Item("STRING2")
                    If IsDBNull(Subject2String) OrElse String.IsNullOrEmpty(Subject2String) Then
                        props.Subject2 = ""
                    Else
                        props.Subject2 = Subject2String
                    End If

                    PlaceString = f.Item("STRING3")
                    If IsDBNull(PlaceString) OrElse String.IsNullOrEmpty(PlaceString) Then
                        props.Place = ""
                    Else
                        props.Place = PlaceString
                    End If

                    DescString = f.Item("STRING4")
                    If IsDBNull(DescString) OrElse String.IsNullOrEmpty(DescString) Then
                        props.Description = ""
                    Else
                        props.Description = DescString
                    End If


                Case "F_AddFormData"
                    Dim f = ClassFunctionCommands.LoadFunction(controlId)

                    props.ControlType = "Function.AddFormData"
                    props.Caption = r.Item("CTRLSCR_CAPTION")

                    If IsDBNull(f.Item("INTEGER1")) Then
                        props.FormID = 0
                    Else
                        props.FormID = NotNullInt(f.Item("INTEGER1"), 0)
                    End If

                    If IsDBNull(f.Item("INTEGER2")) Then
                        props.ScreenID = 0
                    Else
                        props.ScreenID = NotNullInt(f.Item("INTEGER2"), 0)
                    End If
            End Select

            CURRENT_CONTROL_ID = r.Item("CONTROL_ID")
            ' Aktuelle Control Eigenschaften anzeigen
            Me.pgControlProperties.SelectedObject = props
            ' Fenster Titel aktualisieren

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

    Public Sub UpdateControlLocation(ctrl As Control)
        Current_Properties = Me.pgControlProperties.SelectedObject

        If Current_Properties IsNot Nothing Then
            Current_Properties.Location = ctrl.Location
            Me.pgControlProperties.SelectedObject = Current_Properties
        End If

    End Sub

    Public Function SaveControlProperties(ctrl As Control, properties As Object) As Boolean
        If ClassControlCommands.UpdateControl(ctrl, properties) Then
            LoadControlPropertiesNeu(ctrl)
            Return True
        Else
            MsgBox("Fehler beim Speichern der Elementeigenschaften.", MsgBoxStyle.Critical)
            Return False
        End If
    End Function

    Private Sub btnSaveControl_Click(sender As Object, e As EventArgs) Handles btnSaveControl.Click
        If SaveControlProperties(DesignerCtrlBuilder.CurrentControl, pgControlProperties.SelectedObject) Then
            lblStatus.Text = "Änderungen gespeichert - " & Now
            lblStatus.Visible = True
        Else
            lblStatus.Visible = False
        End If
    End Sub

    Private Sub btnDeleteControl_Click(sender As Object, e As EventArgs) Handles btnDeleteControl.Click
        If MsgBox("Wollen Sie das Element wirklich löschen?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If IsGroupBox(DesignerCtrlBuilder.CurrentControl) Then
                Dim parentID As Integer = GetControlID_for_Name(DesignerCtrlBuilder.CurrentControl.Name, CURRENT_ENTITY_ID)
                Dim SQL = "SELECT GUID FROM TBPMO_CONTROL WHERE PARENT_CONTROL_ID = " & parentID
                Dim dt As DataTable = MYDB_ECM.GetDatatable(sql)

                For Each dr As DataRow In dt.Rows
                    Dim id As Integer = dr.Item(0)
                    ClassControlCommands.DeleteControl(id)
                Next

            End If

            If ClassControlCommands.DeleteControl(CURRENT_CONTROL_ID) = True Then
                'MsgBox("Element gelöscht, Elemente werden neu geladen.", MsgBoxStyle.Information)
                DesignerCtrlBuilder.ClearControls()
                frmLevel_Designer.Instance.LoadControls()
            End If
        End If
    End Sub

    Private Sub pgControlProperties_PropertyValueChanged(s As Object, e As PropertyValueChangedEventArgs) Handles pgControlProperties.PropertyValueChanged

        Dim oldValue As Object = e.OldValue
        Dim newValue As Object = e.ChangedItem.Value
        Dim currentProperty As String = e.ChangedItem.Label
        Dim ctrl As Control = DesignerCtrlBuilder.CurrentControl

        Select Case currentProperty
            Case "Location"
                ctrl.Location = newValue
            Case "X"
                ctrl.Location = New Point(newValue, ctrl.Location.Y)
            Case "Y"
                ctrl.Location = New Point(ctrl.Location.X, newValue)
            Case "Size"
                ctrl.Size = newValue
            Case "Width"
                ctrl.Size = New Size(newValue, ctrl.Size.Height)
            Case "Height"
                ctrl.Size = New Size(ctrl.Size.Width, newValue)
            Case "Name"
                ctrl.Name = newValue
            Case "Caption"
                ctrl.Text = newValue
            Case "Font"
                ctrl.Font = newValue
            Case "FontColor"
                If TypeOf ctrl Is ClassControlBuilder.LineLabel Then
                    ctrl.BackColor = newValue
                Else
                    ctrl.ForeColor = newValue
                End If
            Case "BackColor"
                ctrl.BackColor = newValue
        End Select

        Console.WriteLine("Property {0} was changed", e.ChangedItem.Label)

        'lblStatus.Text = "Ausstehende Änderungen"
        'lblStatus.Visible = True

        If SaveControlProperties(DesignerCtrlBuilder.CurrentControl, pgControlProperties.SelectedObject) Then
            If USER_LANGUAGE <> "de-DE" Then
                lblStatus.Text = "Changes saved - " & Now
            Else
                lblStatus.Text = "Änderungen gespeichert - " & Now
            End If
        Else
            lblStatus.Visible = False
        End If
    End Sub

    Private Sub btnTableCheckable_MouseDown(sender As Object, e As MouseEventArgs) Handles btnTableCheckable.MouseDown

    End Sub

    Private Sub btnTableCheckable_MouseMove(sender As Object, e As MouseEventArgs) Handles btnTableCheckable.MouseMove

    End Sub

    Private Sub btnTableCheckable_Click(sender As Object, e As EventArgs) Handles btnTableCheckable.Click

    End Sub
End Class
