Public Class frmTool_ControlProperties
    Private Shared _Instance As frmTool_ControlProperties = Nothing
    Private CurrentProperties As Object = Nothing

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

    Private Sub frmTool_ControlProperties_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ClassWindowLocation.LoadFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmTool_ControlProperties")
    End Sub

    Private Sub frmTool_ControlProperties_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        Try
            ClassWindowLocation.SaveFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmTool_ControlProperties")
            My.Settings.Save()
        Catch ex As Exception
            MsgBox("Settings could not be saved.\n" & ex.ToString)
        End Try
    End Sub

    Public Sub LoadControlPropertiesNeu(ctrl As Control)
        Try

            Dim controlId = DirectCast(ctrl.Tag, ClassControlMetadata).Id
            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 = ClassDatabase.Return_Datatable(sql)
            Dim hint As String = ClassConverter.ToStringOrDefault(ClassDatabase.Execute_Scalar(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 "F_AddAppointment" : props = New FunctionAddAppointment()
                Case "F_AddFormData" : props = New FunctionAddFormData()
            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.Hint = hint

            'Control-spezifische Properties laden
            Select Case type
                Case "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("CTRLSCR_CAPTION")
                    props.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))

                Case "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"), ""))

                Case "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.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.SQLCommand = New SQLValue(NotNull(r.Item("CONTROL_SQLCOMMAND_1"), ""))

                Case "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")

                Case "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))

                Case "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(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")

                Case "Datagridview"
                    ' Keine zusätzliche Properties
                    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.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")

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

                Case "Picturebox"
                    props.IsRequired = r.Item("CONTROL_REQUIRED")
                    props.IsReadOnly = r.Item("CONTROL_READ_ONLY")

                Case "ListBox"
                    props.StaticList = ClassConverter.ToStringOrDefault(r.Item("CONTROL_STATIC_LIST"))
                    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)

                Case "CheckedListBox"
                    props.StaticList = ClassConverter.ToStringOrDefault(r.Item("CONTROL_STATIC_LIST"))
                    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)

                Case "F_AddAppointment"
                    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_FORM_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_FORM_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"
                    props.Caption = r.Item("CTRLSCR_CAPTION")

                    Dim f = ClassFunctionCommands.LoadFunction(controlId)

                    Dim FormId = f.Item("INTEGER1")
                    props.FormID = NotNullInt(FormId, 0)

                    Dim ScreenId = f.Item("INTEGER2")
                    props.ScreenID = NotNullInt(ScreenId, 0)

            End Select

            CURRENT_CONTROL_ID = r.Item("CONTROL_ID")
            ' Aktuelle Control Eigenschaften anzeigen
            Me.pgControlProperties.SelectedObject = props
            ' Fenster Titel aktualisieren
            Me.Text = String.Format("Properties of {0} ({1})", type, props.ID.ToString())

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

    ' ADDED 25.11
    ' Lade Control Eigenschaften und zeige diese an
    ' Wird von frmLevelDesigner aufgerufen
    'Public Sub LoadControlProperties(ctrl As Control)
    '    Try
    '        Dim sql As String = "SELECT CTRLSCR_ID FROM VWPMO_CONTROL_SCREEN WHERE CONTROL_NAME = '" & ctrl.Name & "' AND FORM_ID = " & CURRENT_FORM_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID
    '        Dim CTRLSCR_ID = ClassDatabase.Execute_Scalar(sql)
    '        If CTRLSCR_ID > 0 Then
    '            Dim DT As DataTable = ClassDatabase.Return_Datatable("SELECT * FROM VWPMO_CONTROL_SCREEN WHERE CTRLSCR_ID = " & CTRLSCR_ID)
    '            If DT.Rows.Count = 1 Then
    '                Dim row As DataRow = DT.Rows(0)
    '                Dim type As String = row.Item("CTRLTYPE_NAME")
    '                Dim props As Object = Nothing
    '                Dim fontcolor As Color
    '                Dim fontfamily As String
    '                Dim fontstyle As FontStyle
    '                Dim fontsize As Single
    '                Dim isreadonly As Boolean
    '                Dim isrequired As Boolean
    '                Dim columntitle As String
    '                Dim DefaultValue As String


    '                Select Case type
    '                    Case "Label"
    '                        props = New LabelProperties()
    '                        props.Caption = row.Item("CTRLSCR_CAPTION")
    '                    Case "Textbox"
    '                        props = New TextBoxProperties()
    '                        props.DefaultValue = ClassConverter.ToStringOrDefault(row.Item("CONTROL_DEF_VALUE"))

    '                        If Not IsDBNull(row.Item("CONTROL_FORMAT_TYPE")) Then
    '                            props.Format = DirectCast([Enum].Parse(GetType(EnumFormatOptions), row.Item("CONTROL_FORMAT_TYPE")), Integer)
    '                        Else
    '                            props.Format = "String"
    '                        End If
    '                    Case "Combobox"
    '                        props = New ComboBoxProperties()
    '                        props.MasterDataId = row.Item("CTRLSCR_MASTER_DATA_ID")
    '                        props.DefaultValue = ClassConverter.ToStringOrDefault(row.Item("CONTROL_DEF_VALUE"))

    '                        If Not IsDBNull(row.Item("CONTROL_FORMAT_TYPE")) Then
    '                            props.Format = DirectCast([Enum].Parse(GetType(EnumFormatOptions), row.Item("CONTROL_FORMAT_TYPE")), Integer)
    '                        Else
    '                            props.Format = "String"
    '                        End If

    '                        props.StaticList = ClassConverter.ToStringOrDefault(row.Item("CONTROL_STATIC_LIST"))
    '                    Case "Checkbox"
    '                        props = New CheckBoxProperties()
    '                        props.Caption = row.Item("CTRLSCR_CAPTION")
    '                        props.DefaultValue = ClassConverter.ToBooleanOrDefault(row.Item("CONTROL_DEF_VALUE"))
    '                    Case "Datepicker"
    '                        props = New DateTimePickerProperties()
    '                        props.DefaultValue = NotNull(row.Item("CONTROL_DEF_VALUE"), EnumDateTimePickerDefaultValueOptions.Empty)
    '                        'props.DefaultValue = ClassConverter.ToDateTimePickerOptionsOrDefault(row.Item("CONTROL_DEF_VALUE"))
    '                    Case "Datagridview"
    '                        props = New DataGridViewProperties()
    '                    Case "Groupbox"
    '                        props = New GroupBoxProperties()
    '                        props.Caption = row.Item("CTRLSCR_CAPTION")
    '                        props.BackColor = IntToColor(row.Item("CTRLSCR_BACK_COLOR"))
    '                    Case "Picturebox"
    '                        props = New PictureBoxProperties()
    '                    Case "RadioButton"
    '                        props = New RadioButtonProperties()
    '                        props.Caption = row.Item("CTRLSCR_CAPTION")
    '                        props.DefaultValue = ClassConverter.ToBooleanOrDefault(row.Item("CONTROL_DEF_VALUE"))
    '                        props.IsRequired = row.Item("CONTROL_REQUIRED")
    '                    Case "F_AddAppointment"
    '                        props = New FunctionAddAppointment()
    '                    Case "F_AddFormData"
    '                        props = New FunctionAddFormData()
    '                    Case "CheckedListBox"
    '                        props = New ComboBoxProperties()
    '                        props.StaticList = ClassConverter.ToStringOrDefault(row.Item("CONTROL_STATIC_LIST"))
    '                    Case "ListBox"
    '                        props = New ComboBoxProperties()
    '                        props.StaticList = ClassConverter.ToStringOrDefault(row.Item("CONTROL_STATIC_LIST"))
    '                    Case Else
    '                        MsgBox("Unknown control type " & type, MsgBoxStyle.Exclamation, "Error in LoadControlProperties:")
    '                End Select

    '                ' Generic Properties
    '                props.ID = row.Item("CONTROL_ID")
    '                props.Name = row.Item("CONTROL_NAME")
    '                props.Size = New Size(row.Item("CTRLSCR_WIDTH"), row.Item("CTRLSCR_HEIGHT"))

    '                'TODO: calc location
    '                'If row.Item("CONTROL_PARENT_ID") <> 0 Then
    '                '    Dim parentSQL = "SELECT CTRLSCR_X_LOC, CTRLSCR_Y_LOC FROM VWPMO_VALUES WHERE = CONTROL_ID = " & row.Item("CONTROL_PARENT_ID")
    '                'End If


    '                props.Location = New Point(row.Item("CTRLSCR_X_LOC"), row.Item("CTRLSCR_Y_LOC"))

    '                Me.Text = "Eigenschaften Control-ID: " & props.ID

    '                If type = "F_AddAppointment" Then
    '                    Dim ControlId = GetControlID_for_Name(ctrl.Name, CURRENT_FORM_ID)
    '                    Dim dr = ClassFunctionCommands.LoadFunction(ControlId)

    '                    Dim FromDateId, ToDateId, SubjectString, Subject2String, PlaceString, DescString

    '                    FromDateId = dr.Item("INTEGER1")
    '                    ToDateId = dr.Item("INTEGER2")
    '                    SubjectString = dr.Item("STRING1")
    '                    Subject2String = dr.Item("STRING2")
    '                    PlaceString = dr.Item("STRING3")
    '                    DescString = dr.Item("STRING4")


    '                    If IsDBNull(FromDateId) OrElse FromDateId = 0 Then
    '                        props.FromDate = ""
    '                    Else
    '                        props.FromDate = Get_Name_for_ControlID(FromDateId, CURRENT_FORM_ID)
    '                    End If

    '                    If IsDBNull(ToDateId) OrElse ToDateId = 0 Then
    '                        props.ToDate = ""
    '                    Else
    '                        props.ToDate = Get_Name_for_ControlID(ToDateId, CURRENT_FORM_ID)
    '                    End If



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

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

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

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

    '                    props.Caption = row.Item("CTRLSCR_CAPTION")
    '                ElseIf type = "F_AddFormData" Then
    '                    ' TODO
    '                    Dim ControlId = GetControlID_for_Name(ctrl.Name, CURRENT_FORM_ID)
    '                    Dim dr = ClassFunctionCommands.LoadFunction(ControlId)

    '                    Dim FormId, ScreenId

    '                    FormId = dr.Item("INTEGER1")
    '                    ScreenId = dr.Item("INTEGER2")

    '                    If IsDBNull(FormId) OrElse FormId = 0 Then
    '                        props.FormID = 0
    '                    Else
    '                        props.FormID = FormId
    '                    End If

    '                    If IsDBNull(ScreenId) OrElse ScreenId = 0 Then
    '                        props.ScreenID = 0
    '                    Else
    '                        props.ScreenID = ScreenId
    '                    End If

    '                    props.Caption = row.Item("CTRLSCR_CAPTION")
    '                End If

    '                ' Control Properties
    '                If Not type.Contains("F_") Then
    '                    ' PreSave and Convert Properties
    '                    fontcolor = IntToColor(row.Item("CTRLSCR_FONT_COLOR"))
    '                    fontfamily = row.Item("CTRLSCR_FONT_FAMILY")
    '                    fontstyle = CType(row.Item("CTRLSCR_FONT_STYLE"), FontStyle)
    '                    fontsize = CType(row.Item("CTRLSCR_FONT_SIZE"), Single)
    '                    isreadonly = row.Item("CONTROL_READ_ONLY")
    '                    isrequired = row.Item("CONTROL_REQUIRED")
    '                    columntitle = row.Item("CONTROL_COL_NAME")

    '                    ' Assign Properties to Class
    '                    props.FontColor = fontcolor
    '                    props.Font = New Font(fontfamily, fontsize, fontstyle)
    '                    props.ColumnTitle = columntitle

    '                    'ReadOnly
    '                    If type = "Textbox" Or type = "Datepicker" Or type = "Combobox" Or type = "Picturebox" Or type = "RadioButton" Or type = "Checkbox" Or type = "CheckedListBox" _
    '                        Or type = "ListBox" Then
    '                        props.IsReadOnly = row.Item("CONTROL_READ_ONLY")
    '                    End If

    '                    ' Nicht verfügbar für label
    '                    If type = "Textbox" Or type = "Combobox" Or type = "Datepicker" Or type = "Checkbox" Or type = "RadioButton" Or type = "CheckedListBox" _
    '                     Or type = "ListBox" Then
    '                        props.TabStop = row.Item("CTRLSCR_TAB_STOP")
    '                        props.TabIndex = row.Item("CTRLSCR_TAB_INDEX")

    '                        If Not IsDBNull(row.Item("CONTROL_SHOW_COLUMN")) Then
    '                            props.ShowColumn = row.Item("CONTROL_SHOW_COLUMN")
    '                        Else
    '                            props.ShowColumn = True
    '                        End If
    '                    End If

    '                    If type = "Textbox" Then
    '                        props.Multiline = row.Item("CONTROL_MULTILINE")
    '                    End If

    '                    If type = "Combobox" Or type = "CheckedListBox" Or type = "ListBox" Or type = "Textbox" Then
    '                        If row.Item("CONTROL_SQLCOMMAND_1").ToString.Length > 1 Then
    '                            Dim value As New SQLValue(row.Item("CONTROL_SQLCOMMAND_1").ToString)
    '                            props.SQLCommand = value
    '                            'props.SQLCommand = row.Item("CONTROL_SQLCOMMAND_1").ToString
    '                        End If

    '                        'props.ParentFormID = row.Item("PARENT_FORM_ID")
    '                        props.IsRequired = isrequired
    '                        props.IsReadOnly = isreadonly
    '                    End If

    '                    If type = "Label" Then
    '                        If row.Item("CONTROL_SQLCOMMAND_1").ToString.Length > 1 Then
    '                            Dim value As New SQLValue(row.Item("CONTROL_SQLCOMMAND_1").ToString)
    '                            props.SQLCommand = value
    '                            'props.SQLCommand = row.Item("CONTROL_SQLCOMMAND_1").ToString
    '                        End If
    '                    End If


    '                End If

    '                ' Globale Variable setzen
    '                CURRENT_CONTROL_ID = row.Item("CONTROL_ID")
    '                Me.pgControlProperties.SelectedObject = props
    '            End If
    '        End If

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


    'End Sub

    ' Aktualisiert die Position des akutellen Controls und speichert diese in der Klasse
    Public Sub UpdateControlLocation(ctrl As Control)
        CurrentProperties = Me.pgControlProperties.SelectedObject

        If CurrentProperties IsNot Nothing Then
            CurrentProperties.Location = ctrl.Location
            Me.pgControlProperties.SelectedObject = CurrentProperties
        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(CtrlBuilder.CurrentControl, pgControlProperties.SelectedObject) Then
            lblStatus.Text = "Änderungen gespeichert - " & Now
            lblStatus.Visible = True
        Else
            lblStatus.Visible = False
        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 = CtrlBuilder.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"
                ctrl.ForeColor = newValue
            Case "BackColor"
                ctrl.BackColor = newValue
        End Select

        lblStatus.Text = "Ausstehende Änderungen"
        lblStatus.Visible = True
    End Sub

    Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
        If MsgBox("Wollen Sie das Element wirklich löschen?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            If IsGroupBox(CtrlBuilder.CurrentControl) Then
                Dim parentID As Integer = GetControlID_for_Name(CtrlBuilder.CurrentControl.Name, CURRENT_FORM_ID)
                Dim SQL = "SELECT GUID FROM TBPMO_CONTROL WHERE PARENT_CONTROL_ID = " & parentID
                Dim dt As DataTable = ClassDatabase.Return_Datatable(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() = True Then
                'MsgBox("Element gelöscht, Elemente werden neu geladen.", MsgBoxStyle.Information)
                CtrlBuilder.ClearControls()
                frmLevel_Designer.Instance.LoadControls()
            End If
        End If
    End Sub

    Private Sub frmTool_ControlProperties_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        If Me.WindowState = FormWindowState.Maximized Then
            Me.WindowState = FormWindowState.Normal
        End If
    End Sub
End Class