Public Class frmTaskmanagement
    Private Shared _Instance As frmTaskmanagement = Nothing
    Private insert As Boolean = False
    Public Shared Function Instance() As frmTaskmanagement
        If _Instance Is Nothing OrElse _Instance.IsDisposed = True Then
            _Instance = New frmTaskmanagement
        End If
        _Instance.BringToFront()
        Return _Instance
    End Function
    Private formloaded As Boolean = False
    Private _FORMID As Integer
    Private Sub TBPMO_WORKFLOWBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles TBPMO_WORKFLOWBindingNavigatorSaveItem.Click
        Try
            Me.TBPMO_WORKFLOWBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_WORKFLOW.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox1.Text = Environment.UserName
                Me.TBPMO_WORKFLOWBindingSource.EndEdit()
                Me.TBPMO_WORKFLOWTableAdapter.Update(DD_DMSDataSet.TBPMO_WORKFLOW)
                toolstriplabel(True, "Änderungen Task erfolgreich gespeichert - " & Now)
            Else
                toolstriplabel(False, "")
            End If
        Catch ex As Exception
            MsgBox("Error in Save TBPMO_WORKFLOW:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Sub toolstriplabel(visible As Boolean, txt As String)
        tslbl.Text = txt
        tslbl.Visible = visible

    End Sub

    Private Sub frmTaskmanagement_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ClassWindowLocation.SaveFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmTaskmanagement")
    End Sub
    Private Sub frmTaskmanagement_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            ClassWindowLocation.LoadFormLocationSize(Me, 1, CURRENT_SCREEN_ID, "frmTaskmanagement")
            Me.TBPMO_WORKFLOW_TASK_STATETableAdapter.Connection.ConnectionString = MyConnectionString
            VWPMO_GUI_ENTITYTableAdapter.Connection.ConnectionString = MyConnectionString
            TBPMO_WORKFLOW_FORMTableAdapter.Connection.ConnectionString = MyConnectionString
            TBPMO_WORKFLOWTableAdapter.Connection.ConnectionString = MyConnectionString
            VWPMO_WORKFLOW_FORMTableAdapter.Connection.ConnectionString = MyConnectionString
            Try
                Me.VWPMO_GUI_ENTITYTableAdapter.Fill(Me.DD_DMSDataSet.VWPMO_GUI_ENTITY)
            Catch ex As Exception
                MsgBox("Error in VWPMO_GUI_ENTITYTableAdapter: " & ex.Message, MsgBoxStyle.Exclamation)
                ClassLogger.Add(">> Error in VWPMO_GUI_ENTITYTableAdapter: " & ex.Message, False)
            End Try
            Me.TBPMO_WORKFLOWTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_WORKFLOW)
            Me.TBPMO_WORKFLOW_TASK_STATETableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_WORKFLOW_TASK_STATE)
        Catch ex As Exception
            MsgBox("Error in Load Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
       

    End Sub
    Private Sub Refresh_Workflow_Form(formid As Integer)
        Try
            Me.VWPMO_WORKFLOW_FORMTableAdapter.Fill(Me.DD_DMSDataSet.VWPMO_WORKFLOW_FORM, formid)
        Catch ex As Exception
            MsgBox("Error in Refresh_Workflow_Form:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub TBPMO_WORKFLOWBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_WORKFLOWBindingSource.AddingNew
        DD_DMSDataSet.TBPMO_WORKFLOW.ADDED_WHOColumn.DefaultValue = Environment.UserName
    End Sub

    Private Sub BindingNavigatorDeleteItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorDeleteItem.Click

    End Sub

    Private Sub TBPMO_WORKFLOW_FORMDataGridView_DragDrop(sender As Object, e As DragEventArgs)
        'Try
        '    Dim i As Integer
        '    i = TBPMO_WORKFLOWDataGridView.CurrentRow.Index
        '    If TBPMO_WORKFLOWDataGridView.Item(0, i).Value Is Nothing = False Then
        '        TBPMO_WORKFLOW_FORMTableAdapter.Insert(_FORMID, e.Data.GetData(DataFormats.Text), 1, Environment.UserName)
        '        toolstriplabel(True, "Task erfolgreich zugeordnet - " & Now)
        '        Refresh_Workflow_Form(_FORMID)
        '    End If
        'Catch ex As Exception
        '    MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen eines Users:")
        'End Try
    End Sub

    Private Sub TBPMO_WORKFLOW_FORMDataGridView_DragEnter(sender As Object, e As DragEventArgs)
        ' 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

    Private Sub WH_FormsDataGridView_SelectionChanged(sender As Object, e As EventArgs) Handles WH_FormsDataGridView.SelectionChanged
        If formloaded = True Then
            Dim i As Integer
            i = WH_FormsDataGridView.CurrentRow.Index
            If WH_FormsDataGridView.Item(0, i).Value Is Nothing = False Then
                _FORMID = WH_FormsDataGridView.Item(0, i).Value
                Dim ColName = WH_FormsDataGridView.Item("ColName", i).Value
                Try
                    btnAddTask2Entity.Text = "Task zu Entität '" & ColName.ToString & "' hinzufügen"
                Catch ex As Exception

                End Try
                Refresh_Workflow_Form(_FORMID)
            End If
        End If
    End Sub

    Private Sub frmTaskmanagement_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        formloaded = True
    End Sub
    Private Sub ToolStripButton14_Click(sender As Object, e As EventArgs) Handles ToolStripButton14.Click
        Try
            Me.TBPMO_WORKFLOW_FORMBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_WORKFLOW_FORM.GetChanges Is Nothing = False Then
                Me.TBPMO_WORKFLOW_FORMTableAdapter.Update(DD_DMSDataSet.TBPMO_WORKFLOW_FORM)
                toolstriplabel(True, "Änderungen erfolgreich gespeichert - " & Now)
            Else
                toolstriplabel(False, "")
            End If
        Catch ex As Exception
            MsgBox("Error in Save TBPMO_WORKFLOW_FORM:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub

    Private Sub ToolStripButton18_Click(sender As Object, e As EventArgs) Handles ToolStripButton18.Click
        Save_State()
    End Sub
    Sub Save_State()
        Try
            Me.TBPMO_WORKFLOW_TASK_STATEBindingSource.EndEdit()
            If DD_DMSDataSet.TBPMO_WORKFLOW_TASK_STATE.GetChanges Is Nothing = False Then
                Me.CHANGED_WHOTextBox.Text = Environment.UserName
                Me.TBPMO_WORKFLOW_TASK_STATEBindingSource.EndEdit()
                Me.TBPMO_WORKFLOW_TASK_STATETableAdapter.Update(DD_DMSDataSet.TBPMO_WORKFLOW_TASK_STATE)
                toolstriplabel(True, "Änderungen erfolgreich gespeichert - " & Now)
            End If
        Catch ex As Exception
            MsgBox("Error in Save TBPMO_WORKFLOW_TASK_STATE:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try

    End Sub
    Private Sub TBPMO_WORKFLOW_TASK_STATEBindingSource_AddingNew(sender As Object, e As System.ComponentModel.AddingNewEventArgs) Handles TBPMO_WORKFLOW_TASK_STATEBindingSource.AddingNew
        DD_DMSDataSet.TBPMO_WORKFLOW_TASK_STATE.CHANGED_WHOColumn.DefaultValue = Environment.UserName
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnAddTask2Entity.Click
        Try
            Dim i As Integer
            i = WH_FormsDataGridView.CurrentRow.Index
            If WH_FormsDataGridView.Item(0, i).Value Is Nothing = False Then
                _FORMID = WH_FormsDataGridView.Item(0, i).Value
                TBPMO_WORKFLOW_FORMTableAdapter.Insert(_FORMID, GUIDTextBox1.Text, 1, Environment.UserName)
                toolstriplabel(True, "Task erfolgreich zugeordnet - " & Now)
                Refresh_Workflow_Form(_FORMID)
            End If


        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Hinzufügen Task zu Entität:")
        End Try
    End Sub

    Private Sub btncolordialog_Click(sender As Object, e As EventArgs) Handles btncolordialog.Click
        If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
            Dim colour As Integer = ColorTranslator.ToWin32(ColorDialog1.Color)
            COLORTextBox.Text = colour
            COLORTextBox.BackColor = ColorDialog1.Color
            Save_State()
        End If
    End Sub
  

    Private Sub ListBox2_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox2.DrawItem
        Dim clrSelectedText As Color = Color.Red    'Our color for selected text
        Dim clrHighlight As Color = Color.Yellow    'Our background for selected items
        If e.State = DrawItemState.Selected Then
            'This item is selected
            'e.DrawBackground()
            e.Graphics.FillRectangle(New SolidBrush(clrHighlight), e.Bounds)    'Fill the item's rectangle with our highlight
            e.Graphics.DrawString(ListBox2.Items.Item(e.Index), e.Font, New SolidBrush(clrSelectedText), e.Bounds)  'Draw the text for the item
        ElseIf e.State = DrawItemState.None Then
            'This item has no state
            e.DrawBackground()  'Draw our regular background
            e.Graphics.DrawString(ListBox2.Items.Item(e.Index), e.Font, Brushes.Black, e.Bounds)    'Draw the item text in its regular color
        End If
        'Draws a focus rectangle around the item if it has focus
        e.DrawFocusRectangle()
    End Sub

    Private Sub COLORTextBox_TextChanged(sender As Object, e As EventArgs) Handles COLORTextBox.TextChanged
        Try
            COLORTextBox.BackColor = ColorTranslator.FromWin32(CInt(COLORTextBox.Text))
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
End Class