Public Class frmQuickAccessManager

    Sub New()
        InitializeComponent()
    End Sub

    Private Sub frmQuickAccessManager_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        ' frmMain.LoadMenuItems()
    End Sub

    Private Sub frmImageManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        RefreshData()
    End Sub

    Private Sub RefreshData()
        ImageTableAdapter.Connection.ConnectionString = MyConnectionString
        Me.ImageTableAdapter.Fill(Me.DD_DMSDataSet.TBPMO_FORM_CONSTRUCTOR, USER_LANGUAGE, CURRENT_SCREEN_ID, USER_GUID)
    End Sub

    Private Sub btnSelectImage_Click(sender As Object, e As EventArgs) Handles btnSelectImage.Click
        OpenFileDialog1.Filter = "PNG Bilder|*.png"

        If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim path As String = OpenFileDialog1.FileName
            pbNew.ImageLocation = path
            btnSaveImage.Enabled = True
        End If
    End Sub

    Private Sub btnSaveImage_Click(sender As Object, e As EventArgs) Handles btnSaveImage.Click
        Try
            Dim image As Bitmap = CType(Drawing.Image.FromFile(pbNew.ImageLocation, True), Bitmap)
            Dim bimage() As Byte = BitmapToByteArray(image)
            Dim guid As String = gvMainView.GetFocusedRowCellValue(gvMainView.Columns(0).FieldName)
            'UPDATE TBPMO_FORM_CONSTRUCTOR SET MENU_IMG = @MENU_IMG, WHERE GUID = @GUID"
            Dim SQL As String = "UPDATE TBPMO_FORM_CONSTRUCTOR SET MENU_IMG = @MENU_IMG WHERE GUID = @GUID"
            Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(MyConnectionString)
            Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(SQL, conn)

            cmd.Parameters.Add("@MENU_IMG", SqlDbType.VarBinary).Value = bimage
            cmd.Parameters.Add("@GUID", SqlDbType.Int).Value = guid

            conn.Open()
            'ClassDatabase.Execute_non_Query(SQL, True)
            cmd.ExecuteNonQuery()
            conn.Close()

            pbNew.Image = Nothing

            btnSaveImage.Enabled = False

            RefreshData()
        Catch ex As Exception
            MsgBox("Fehler beim Speichern des Icons:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnDeleteImage_Click(sender As Object, e As EventArgs) Handles btnDeleteImage.Click
        Try
            Dim SQL As String = "UPDATE TBPMO_FORM_CONSTRUCTOR SET MENU_IMG = @MENU_IMG WHERE GUID = @GUID"
            Dim conn As SqlClient.SqlConnection = New SqlClient.SqlConnection(MyConnectionString)
            Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand(SQL, conn)
            Dim guid As String = gvMainView.GetFocusedRowCellValue(gvMainView.Columns(0).FieldName)

            cmd.Parameters.Add("@MENU_IMG", SqlDbType.VarBinary).Value = DBNull.Value
            cmd.Parameters.Add("@GUID", SqlDbType.Int).Value = guid

            conn.Open()
            cmd.ExecuteNonQuery()
            conn.Close()

            btnSaveImage.Enabled = False

            RefreshData()
        Catch ex As Exception
            MsgBox("Fehler beim Löschen des Icons:" & vbNewLine & ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        Try
            'ImageBindingSource.EndEdit()
            'If Me.DD_DMSDataSet.TBPMO_FORM_CONSTRUCTOR.GetChanges Is Nothing = False Then
            '    Me.ImageTableAdapter.Update(Me.DD_DMSDataSet.TBPMO_FORM_CONSTRUCTOR)
            'End If

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

    Private Sub ToolStrip1_ItemClicked(sender As Object, e As ToolStripItemClickedEventArgs) Handles ToolStrip1.ItemClicked

    End Sub

    Private Sub gvMainView_CellValueChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles gvMainView.CellValueChanged
        Try
            Dim column = e.Column.FieldName
            Dim id = gvMainView.GetFocusedRowCellValue(gvMainView.Columns("GUID"))
            Dim sql = String.Format("UPDATE TBPMO_FORM_CONSTRUCTOR SET {0} = '{1}',CHANGED_WHO = '{2}',LANGUAGE = '{3}' where GUID = {4}", column, e.Value, Environment.UserName, USER_LANGUAGE, id)
            If ClassDatabase.Execute_non_Query(sql) = False Then
                MsgBox("Update Value not successfull. Check logfile", MsgBoxStyle.Critical)
            End If

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