Imports DevExpress.XtraGrid.Views.Tile
Imports DevExpress.XtraEditors

Public Class frmConstructorDesigner
    Dim SELECTED_ENTITY_ID As Integer
    Dim SELECTED_CONSTRUCTOR_DETAIL_ID As Integer
    Dim formloaded As Boolean = False
    Private Sub TBPMO_FORM_CONSTRUCTORBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_FORM_CONSTRUCTORBindingNavigatorSaveItem.Click
        Try
            Me.Validate()
            Me.TBPMO_FORM_CONSTRUCTORBindingSource.EndEdit()
            If DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = Environment.UserName
                TBPMO_FORM_CONSTRUCTORTableAdapter.Update(DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR)
                lblSave.Visible = True
            Else
                lblSave.Visible = False
            End If

        Catch ex As Exception
            MsgBox("Error in Saving FormConstructor:" & vbNewLine & ex.Message)
        End Try


    End Sub

    Private Sub frmConstructorDesigner_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: Diese Codezeile lädt Daten in die Tabelle "DD_ECMAdmin1.TBPMO_FORM_CONSTRUCTOR". Sie können sie bei Bedarf verschieben oder entfernen.
        Try
            XtraTabControl2.SelectedTabPageIndex = 0
            formloaded = False
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Connection.ConnectionString = MyConnectionString
            Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Connection.ConnectionString = MyConnectionString
            Load_Head()
            Dim sql1 As String = "SELECT T.GUID, T1.FORM_TITLE FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1 ORDER BY T1.FORM_TITLE"
            Dim DTFORMS As DataTable = ClassDatabase.Return_Datatable(sql1, "")
            'FORM_IDComboBox.DataSource = DTFORMS
            'FORM_IDComboBox.DisplayMember = DTFORMS.Columns(1).ColumnName
            'FORM_IDComboBox.ValueMember = DTFORMS.Columns(0).ColumnName

            Dim sql2 As String = "SELECT T.GUID, T1.FORM_TITLE FROM TBPMO_FORM T, TBPMO_FORM_VIEW T1 WHERE T.FORM_TYPE_ID IN (1,2) AND T.GUID = T1.FORM_ID AND T.SHOW_FORM_CONSTRUCT = 1 AND T1.SCREEN_ID = 1 ORDER BY T1.FORM_TITLE"
            Dim DTParent As DataTable = ClassDatabase.Return_Datatable(sql2, "")
            'PARENT_IDComboBox.DataSource = DTParent
            'PARENT_IDComboBox.DisplayMember = DTParent.Columns(1).ColumnName
            'PARENT_IDComboBox.ValueMember = DTParent.Columns(0).ColumnName

        Catch ex As Exception
            MsgBox("Error Loading Form:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Sub Load_Head()
        Try
            Me.TBPMO_FORM_CONSTRUCTORTableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR)
        Catch ex As Exception
            MsgBox("Error Loading Form:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Private Sub Load_Detail(ID As Integer)
        Try
            Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Fill(Me.DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL, ID)
            SELECTED_ENTITY_ID = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & ID, True)
        Catch ex As System.Exception
            MsgBox("Error Loading Details:" & vbNewLine & ex.Message)
        End Try
        Select Case XtraTabControl2.SelectedTabPageIndex
            Case 0
                Me.btnSetHeadline.Visible = False
                Load_QuickDisplay()
            Case 2
                RecordSearch_Config()
        End Select

    End Sub
    Public Sub Load_QuickDisplay()
        Try
            If SELECTED_CONSTRUCTOR_DETAIL_ID = 0 Then Exit Sub
            Dim formId = ClassDatabase.Execute_Scalar("SELECT FORM_ID FROM TBPMO_FORM_CONSTRUCTOR_DETAIL WHERE GUID = " & SELECTED_CONSTRUCTOR_DETAIL_ID, True)
            Dim sql = "SELECT T.GUID, T.COL_NAME AS 'COL_NAME' FROM TBPMO_CONTROL T, TBPMO_CONTROL_SCREEN T1, TBPMO_CONTROL_TYPE T2 WHERE T.SHOW_COLUMN = 1 AND T.[COL_NAME] <> '' AND T.GUID = T1.CONTROL_ID AND T.CONTROL_TYPE_ID = T2.GUID AND T.FORM_ID = " & formId & " AND LEN(ISNULL(T.COL_NAME,'')) > 0 and t.CONTROL_TYPE_ID not in (1,5,12) " & _
                                "AND T2.GUID not in (1,5,7,8,9) AND T.GUID NOT IN (SELECT CONTROL_ID FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & ") ORDER BY T1.TAB_INDEX"
            Dim DT As DataTable = ClassDatabase.Return_Datatable(sql, "LOAD QUICKDISPLAY")
            listBoxControls.DataSource = DT
            listBoxControls.DisplayMember = DT.Columns(1).ColumnName
            listBoxControls.ValueMember = DT.Columns(0).ColumnName

            Dim DT2 = ClassDatabase.Return_Datatable("SELECT T.GUID, T1.COL_NAME FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY T, TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID <> 6 AND T.CONTROL_ID = T1.GUID AND T.CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " ORDER BY T.SEQUENCE", "LOAD QUICKDISPLAY")
            ListBoxConfiguredDisplay.DataSource = DT2
            ListBoxConfiguredDisplay.DisplayMember = DT2.Columns(1).ColumnName
            ListBoxConfiguredDisplay.ValueMember = DT2.Columns(0).ColumnName

            Dim DT3 As DataTable = ClassDatabase.Return_Datatable("SELECT T.GUID, T1.COL_NAME FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY T, TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID = 6 AND T.CONTROL_ID = T1.GUID AND T.CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID, "LOAD QUICKDISPLAY PICTURE")
            If DT3.Rows.Count = 1 Then
                Me.txtQuickViewPicture.Text = DT3.Rows(0).Item(1)
                Me.txtQuickViewPicture.Tag = DT3.Rows(0).Item(0)
            Else
                Me.txtQuickViewPicture.Text = "NOT CONFIGURED"
                Me.txtQuickViewPicture.Tag = Nothing
            End If
            Try
                Me.TBTEMP_QUICKDISPLAYTableAdapter.Connection.ConnectionString = MyConnectionString
                Me.TBTEMP_QUICKDISPLAYTableAdapter.Fill(Me.DD_DMSDataSet.TBTEMP_QUICKDISPLAY, SELECTED_CONSTRUCTOR_DETAIL_ID)
            Catch ex As System.Exception
                MsgBox("Error in Load_Preview_QuickDisplay:" & vbNewLine & ex.Message)
            End Try
        Catch ex As Exception
            MsgBox("Error Load_QuickDisplay:" & vbNewLine & ex.Message)
        End Try
    End Sub
    Public Sub RecordSearch_Config()
        Try
            'Zurücksetzen
            txtWindreamsuche.Text = ""
            cmbSearchPattern1.SelectedIndex = -1
            cmbSearchPattern2.SelectedIndex = -1
            cmbSearchPattern3.SelectedIndex = -1
            cmbSearchPattern4.SelectedIndex = -1
            cmbSearchPattern5.SelectedIndex = -1
            Dim res = ClassDatabase.Execute_Scalar("SELECT DOCUMENT_VIEW FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)

            If res = True Then
                chkWDSearch_Active.Checked = True

                Dim DT As DataTable = ClassDatabase.Return_Datatable("SELECT WINDREAM_SEARCH,SEARCH_PATTERN1,SEARCH_PATTERN2,SEARCH_PATTERN3,SEARCH_PATTERN4,SEARCH_PATTERN5 FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)
                If DT.Rows.Count = 1 Then
                    If Not IsDBNull(DT.Rows(0).Item(0)) Then
                        txtWindreamsuche.Text = ClassHelper.CheckDBNull(DT.Rows(0).Item(0), "String")
                        'Form-ID (Entity)
                        'Record -ID
                        Dim result
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(1), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern1.SelectedIndex = 1
                            Else
                                cmbSearchPattern1.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(2), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern2.SelectedIndex = 1
                            Else
                                cmbSearchPattern2.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(3), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern3.SelectedIndex = 1
                            Else
                                cmbSearchPattern3.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(4), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern4.SelectedIndex = 1
                            Else
                                cmbSearchPattern4.SelectedIndex = 0
                            End If
                        End If
                        result = ClassHelper.CheckDBNull(DT.Rows(0).Item(5), "String")
                        If result <> "" Then
                            If result.ToString.ToUpper.Contains("RECORD") Then
                                cmbSearchPattern5.SelectedIndex = 1
                            Else
                                cmbSearchPattern5.SelectedIndex = 0
                            End If
                        End If
                        'cmbSearchPattern1.SelectedIndex = cmbSearchPattern1.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(1), "String"))
                        'cmbSearchPattern2.SelectedIndex = cmbSearchPattern2.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(2), "String"))
                        'cmbSearchPattern3.SelectedIndex = cmbSearchPattern3.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(3), "String"))
                        'cmbSearchPattern4.SelectedIndex = cmbSearchPattern4.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(4), "String"))
                        'cmbSearchPattern5.SelectedIndex = cmbSearchPattern5.FindStringExact(ClassHelper.CheckDBNull("@" & DT.Rows(0).Item(5), "String"))
                    End If
                End If
            Else
                chkWDSearch_Active.Checked = False
            End If
        Catch ex As Exception
            MsgBox("Error in RecordSearch_Config:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub frmConstructorDesigner_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        formloaded = True
        Load_TreeViewEbene()
    End Sub
    Sub Load_TreeViewEbene()
        Try
            Dim CONSTR_ID As Integer = CONSTRUCTOR_IDTextBox.Text
            Dim DT As DataTable = ClassDatabase.Return_Datatable("SELECT * from VWPMO_CONSTRUCTOR_FORMS WHERE FORM_TYPE_ID = 1 AND PARENT_ID = 0 AND FORM_TYPE_ID = 1 AND CONSTRUCT_ID = " & CONSTR_ID)
            If DT.Rows.Count >= 1 Then
                TreeViewDetails.Nodes.Clear()
                For Each rowEbene0 As DataRow In DT.Rows
                    Dim Node As TreeNode
                    Node = TreeViewDetails.Nodes.Add("E1: " & rowEbene0.Item("FORM_TITLE").ToString)
                    Node.Tag = rowEbene0.Item("GUID")
                    'Jetzt die Ebene1Knoten einhängen
                    Dim dtEbene1 As DataTable = ClassDatabase.Return_Datatable("SELECT GUID, CONSTRUCT_ID, FORM_ID, FORM_TITLE FROM VWPMO_CONSTRUCTOR_FORMS WHERE PARENT_ID = " & rowEbene0.Item("FORM_ID") & _
                                                                               " AND FORM_TYPE_ID in (2,5) AND CONSTRUCT_ID = " & CONSTR_ID & " ORDER BY SEQUENCE")
                    If dtEbene1.Rows.Count > 0 Then
                        For Each rowEbene1 As DataRow In dtEbene1.Rows
                            Dim Node1 As TreeNode
                            Node1 = Node.Nodes.Add("E2: " & rowEbene1.Item("FORM_TITLE").ToString)
                            Node1.Tag = rowEbene1.Item("GUID")
                            'Jetzt die Ebene2 Knoten einhängen
                            Dim dtEbene2 As DataTable = ClassDatabase.Return_Datatable("SELECT GUID, CONSTRUCT_ID, FORM_ID, FORM_TITLE FROM VWPMO_CONSTRUCTOR_FORMS WHERE PARENT_ID = " & rowEbene1.Item("FORM_ID") & _
                                                                                       " AND FORM_TYPE_ID in (3,5) AND CONSTRUCT_ID = " & CONSTR_ID & " ORDER BY SEQUENCE")
                            If dtEbene2.Rows.Count > 0 Then
                                For Each rowEbene2 As DataRow In dtEbene2.Rows
                                    Dim Node2 As TreeNode
                                    Node2 = Node1.Nodes.Add("E3: " & rowEbene2.Item("FORM_TITLE").ToString)
                                    Node2.Tag = rowEbene2.Item("GUID")
                                Next
                            End If

                        Next

                    End If
                Next
                TreeViewDetails.ExpandAll()
                TreeViewDetails.Nodes(0).EnsureVisible()
            Else
                MsgBox("Achtung Konfigurationsfehler: Mehrere oder keine Level 0 Entität gefunden!" & "Korrigieren Sie bitte Ihre Eingaben", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
            MsgBox("Error in Load_TreeView:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub TBPMO_FORM_CONSTRUCTORBindingSource_CurrentChanged(sender As Object, e As EventArgs) Handles TBPMO_FORM_CONSTRUCTORBindingSource.CurrentChanged
        lblSave.Visible = False
        If formloaded = True Then
            Try
                If CONSTRUCTOR_IDTextBox.Text <> "" Then
                    Load_TreeViewEbene()
                End If
            Catch ex As Exception
                MsgBox("Error Loading Construct-Data:" & vbNewLine & ex.Message)
            End Try
        End If
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeViewDetails.AfterSelect
        Try
            If Not TreeViewDetails.SelectedNode Is Nothing Then
                Dim construct_id As Integer = Integer.Parse(TreeViewDetails.SelectedNode.Tag)
                SELECTED_CONSTRUCTOR_DETAIL_ID = construct_id
                Me.tslblSaveDetail.Visible = False
                Load_Detail(construct_id)
                lblSave1.Visible = False

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

    Private Sub LEVEL1_SELECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LEVEL1_SELECTCheckBox.CheckedChanged
        Try
            If LEVEL1_SELECTCheckBox.Checked Then
                SQL_SELECT_EBENE1TextBox.Enabled = True
            Else
                SQL_SELECT_EBENE1TextBox.Enabled = False
            End If
            Dim view As String
            If SQL_SELECT_EBENE1TextBox.Text <> "" Then
                view = SQL_SELECT_EBENE1TextBox.Text.ToUpper.Replace("SELECT * FROM ", "")
                view = view.Trim
                Dim Sql = "SELECT name FROM sys.columns WHERE object_id = OBJECT_ID('" & view & "')"
                If LogErrorsOnly = False Then ClassLogger.Add(">> " & Sql, False)
                Dim DT As DataTable = ClassDatabase.Return_Datatable(Sql)
                If DT IsNot Nothing Then
                    COLUMN_NAME1ComboBox.Items.Clear()
                    For Each row As DataRow In DT.Rows
                        COLUMN_NAME1ComboBox.Items.Add(row.Item(0))
                    Next
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in LEVEL1_SELECTCheckBox:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub tsbtnSave_Click(sender As Object, e As EventArgs) Handles tsbtnSave.Click
        Try
            Me.TBPMO_FORM_CONSTRUCTOR_DETAILBindingSource.EndEdit()
            If DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox1.Text = Environment.UserName
                Me.TBPMO_FORM_CONSTRUCTOR_DETAILBindingSource.EndEdit()
                Me.TBPMO_FORM_CONSTRUCTOR_DETAILTableAdapter.Update(DD_ECMAdmin.TBPMO_FORM_CONSTRUCTOR_DETAIL)
                tslblSaveDetail.Text = "Constructor-Details erfolgreich gespeichert - " & Now
                Me.tslblSaveDetail.Visible = True
            Else
                Me.tslblSaveDetail.Visible = False
            End If

        Catch ex As Exception
            MsgBox("Error in Save Constructor-Details:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
        Try
            Try
                Dim SP1, SP2, SP3, SP4, SP5
                If cmbSearchPattern1.SelectedIndex <> -1 Then
                    SP1 = "@" & cmbSearchPattern1.Text
                Else
                    SP1 = ""
                End If
                If cmbSearchPattern2.SelectedIndex <> -1 Then
                    SP2 = "@" & cmbSearchPattern2.Text
                Else
                    SP2 = ""
                End If
                If cmbSearchPattern3.SelectedIndex <> -1 Then
                    SP3 = "@" & cmbSearchPattern3.Text
                Else
                    SP3 = ""
                End If
                If cmbSearchPattern4.SelectedIndex <> -1 Then
                    SP4 = "@" & cmbSearchPattern4.Text
                Else
                    SP4 = ""
                End If
                If cmbSearchPattern5.SelectedIndex <> -1 Then
                    SP5 = "@" & cmbSearchPattern5.Text
                Else
                    SP5 = ""
                End If
                Dim sql = "UPDATE TBPMO_FORM_VIEW SET WINDREAM_SEARCH = '" & txtWindreamsuche.Text & "',SEARCH_PATTERN1 = '" & SP1 & "',SEARCH_PATTERN2= '" & SP2 & "',SEARCH_PATTERN3= '" & SP3 & _
                         "',SEARCH_PATTERN4 = '" & SP4 & "',SEARCH_PATTERN5 = '" & SP5 & "', CHANGED_WHO = '" & Environment.UserName & "' where FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID
                If ClassDatabase.Execute_non_Query(sql) = True Then
                    lblSave1.Visible = True
                    lblSave1.Text = "Erfolgreich gespeichert - " & Now
                End If
            Catch ex As Exception
                MsgBox("Error in Save DocView-properties:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                lblSave1.Visible = False
            End Try
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Not Me.txtWindreamsuche.Text = "" Then
            Me.OFDWindreamsuche.FileName = Me.WINDREAM_SEARCHTextBox.Text
        End If

        If Me.OFDWindreamsuche.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.WINDREAM_SEARCHTextBox.Text = Me.OFDWindreamsuche.FileName
        End If
    End Sub

    Private Sub btnWindreamsuche_Click(sender As Object, e As EventArgs) Handles btnWindreamsuche.Click
        If Not Me.txtWindreamsuche.Text = "" Then
            Me.OFDWindreamsuche.FileName = Me.txtWindreamsuche.Text
        End If

        If Me.OFDWindreamsuche.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.txtWindreamsuche.Text = Me.OFDWindreamsuche.FileName
        End If
    End Sub

    Private Sub chkWDSearch_Active_CheckedChanged(sender As Object, e As EventArgs) Handles chkWDSearch_Active.CheckedChanged
        Try
            Dim check = ClassDatabase.Execute_Scalar("SELECT DOCUMENT_VIEW FROM TBPMO_FORM_VIEW WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID)

            If chkWDSearch_Active.Checked Then
                grpbxWD_RecordSearch.Enabled = True
                If check = False Then
                    ClassDatabase.Execute_Scalar("UPDATE TBPMO_FORM_VIEW SET DOCUMENT_VIEW = 1 WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID, True)
                End If
            Else
                grpbxWD_RecordSearch.Enabled = False
                If check = True Then
                    ClassDatabase.Execute_Scalar("UPDATE TBPMO_FORM_VIEW SET DOCUMENT_VIEW = 1 WHERE FORM_ID = " & SELECTED_ENTITY_ID & " and SCREEN_ID = " & CURRENT_SCREEN_ID, True)
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Checked Change WD-Search:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub XtraTabControl2_SelectedPageChanged(sender As Object, e As DevExpress.XtraTab.TabPageChangedEventArgs) Handles XtraTabControl2.SelectedPageChanged
        Select Case XtraTabControl2.SelectedTabPageIndex
            Case 0
                Load_QuickDisplay()
            Case 2
                RecordSearch_Config()
        End Select
    End Sub

    Private Sub btnAddControl_Click(sender As Object, e As EventArgs) Handles btnAddControl.Click
        Try
            Dim i = listBoxControls.SelectedValue
            If Not IsNothing(i) Then
                Dim insert = "INSERT INTO TBPMO_FORM_CONSTR_QUICK_DISPLAY (CONSTRUCT_ID, CONTROL_ID,ADDED_WHO) VALUES (" & SELECTED_CONSTRUCTOR_DETAIL_ID & ", " & i & ",'" & Environment.UserName & "')"
                If ClassDatabase.Execute_non_Query(insert, True) Then
                    Load_QuickDisplay()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Adding Control to Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btndeleteControl_Click(sender As Object, e As EventArgs) Handles btndeleteControl.Click
        Try
            Dim i = ListBoxConfiguredDisplay.SelectedValue
            If Not IsNothing(i) Then
                Dim delete = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY  WHERE GUID = " & i
                If ClassDatabase.Execute_non_Query(delete, True) Then
                    Load_QuickDisplay()
                End If
            End If
        Catch ex As Exception
            MsgBox("Error in Deleting Control from Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub ListBoxConfiguredDisplay_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBoxConfiguredDisplay.SelectedIndexChanged
        lblChangeHeadline.Visible = False
        If ListBoxConfiguredDisplay.SelectedIndex <> -1 Then
            Try
                Dim txt As String = ListBoxConfiguredDisplay.Text
                Me.btnSetHeadline.Text = "Set '" & txt & "'" & vbNewLine & "as Headline"
                Me.btnSetHeadline.Visible = True
            Catch ex As Exception

            End Try

        End If
    End Sub

    Private Sub btnSetHeadline_Click(sender As Object, e As EventArgs) Handles btnSetHeadline.Click
        Dim upd = "UPDATE TBPMO_FORM_CONSTR_QUICK_DISPLAY SET HEADLINE = 1, CHANGED_WHO = '" & Environment.UserName & "' WHERE GUID = " & ListBoxConfiguredDisplay.SelectedValue
        If ClassDatabase.Execute_non_Query(upd, True) Then
            lblChangeHeadline.Visible = True
        End If
    End Sub

    Private Sub btnSetQuickViewPicture_Click(sender As Object, e As EventArgs) Handles btnSetQuickViewPicture.Click
        Dim DEL As String = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " AND CONTROL_ID in (SELECT GUID FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 6)"
        If ClassDatabase.Execute_non_Query(DEL, True) Then
            Try
                Dim i = listBoxControls.SelectedValue
                If Not IsNothing(i) Then
                    Dim insert = "INSERT INTO TBPMO_FORM_CONSTR_QUICK_DISPLAY (CONSTRUCT_ID, CONTROL_ID,PICTURE,ADDED_WHO) VALUES (" & SELECTED_CONSTRUCTOR_DETAIL_ID & ", " & i & ",1,'" & Environment.UserName & "')"
                    If ClassDatabase.Execute_non_Query(insert, True) Then
                        Load_QuickDisplay()
                        lblQuickViewAdded.Visible = True
                    Else
                        lblQuickViewAdded.Visible = False
                    End If
                End If
            Catch ex As Exception
                MsgBox("Error in Adding Control to Quick-Display: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                lblQuickViewAdded.Visible = False
            End Try
        End If
    End Sub

    Private Sub listBoxControls_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listBoxControls.SelectedIndexChanged
        If listBoxControls.SelectedIndex <> -1 Then
            Try
                Dim i = listBoxControls.SelectedValue
                If i.ToString.Contains("DataRowV") Then Exit Sub
                If Not IsNothing(i) Then
                    Dim count = ClassDatabase.Execute_Scalar("SELECT COUNT(T1.GUID) FROM TBPMO_CONTROL T1 WHERE T1.CONTROL_TYPE_ID = 6 and T1.GUID = " & i, True)
                    If count = 1 Then
                        Me.btnSetQuickViewPicture.Visible = True
                    Else
                        Me.btnSetQuickViewPicture.Visible = False
                    End If
                End If
                lblQuickViewAdded.Visible = False
            Catch ex As Exception
                MsgBox("Error in CHECK CONTROL IS PICTURE: " & vbNewLine & ex.Message, MsgBoxStyle.Critical)
                Me.btnSetQuickViewPicture.Visible = False
            End Try
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim DEL As String = "DELETE FROM TBPMO_FORM_CONSTR_QUICK_DISPLAY WHERE CONSTRUCT_ID = " & SELECTED_CONSTRUCTOR_DETAIL_ID & " AND CONTROL_ID in (SELECT GUID FROM TBPMO_CONTROL WHERE CONTROL_TYPE_ID = 6)"
        If ClassDatabase.Execute_non_Query(DEL, True) Then
            Load_QuickDisplay()
        End If
    End Sub
    Private Function CreateTable(ByVal rowCount As Integer) As DataTable
        Dim tbl As New DataTable()
        tbl.Columns.Add("Name", GetType(String))
        tbl.Columns.Add("ID", GetType(Integer))
        tbl.Columns.Add("Image", GetType(Image))
        tbl.Columns.Add("Date", GetType(Date))
        For i As Integer = 0 To rowCount - 1
            tbl.Rows.Add(New Object() {String.Format("Name{0}", i), i, ImageCollection1.Images(i Mod 4), Date.Now.AddDays(i - 3)})
        Next i
        Return tbl
    End Function

    Private Sub LOAD_DIRECTCheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LOAD_DIRECTCheckBox.CheckedChanged

    End Sub
End Class