Imports ThoughtWorks.QRCode
Imports ThoughtWorks.QRCode.Codec
Imports ThoughtWorks.QRCode.Codec.Data
Imports System.Data
Imports System.Data.OleDb

Public Class formLesenGenerierenCodeQR
    Dim MyState As String, MyBookmark As Integer
    Dim ProjektConnection As OleDbConnection
    Dim ProjektCommand As OleDbCommand
    Dim ProjektAdapter As OleDbDataAdapter
    Dim ProjektTable As DataTable
    Dim ProjektManager As CurrencyManager
    Private colorFondoQR As Integer =
        Color.FromArgb(255, 255, 255, 255).ToArgb
    Private colorQR As Integer =
        Color.FromArgb(255, 0, 0, 0).ToArgb

    Private Function AdjustQRVersion(ByVal TextEncode As String,
             ByVal qrCodeErrCorrect As _
             Codec.QRCodeEncoder.ERROR_CORRECTION) As Integer
        Dim textb() As Byte =
            System.Text.UTF8Encoding.UTF8.GetBytes(TextEncode)
        Dim ibits As Integer = ((textb.Length + 1) * 8) + 8

        Return -1

        Select Case qrCodeErrCorrect
            'Corrección de errores del 7%
            Case QRCodeEncoder.ERROR_CORRECTION.L
                Select Case ibits
                    Case Is <= 152
                        Return 1
                    Case Is <= 272
                        Return 2
                    Case Is <= 440
                        Return 3
                    Case Is <= 640
                        Return 4
                    Case Is <= 864
                        Return 5
                    Case Is <= 1088
                        Return 6
                    Case Is <= 1248
                        Return 7
                    Case Is <= 1552
                        Return 8
                    Case Is <= 1856
                        Return 9
                    Case Is <= 2192
                        Return 10
                    Case Is <= 2592
                        Return 11
                    Case Is <= 2960
                        Return 12
                    Case Is <= 3424
                        Return 13
                    Case Is <= 3688
                        Return 14
                    Case Is <= 4184
                        Return 15
                    Case Is <= 4712
                        Return 16
                    Case Is <= 5176
                        Return 17
                    Case Is <= 5768
                        Return 18
                    Case Is <= 6360
                        Return 19
                    Case Is <= 6888
                        Return 20
                    Case Is <= 7456
                        Return 21
                    Case Is <= 8048
                        Return 22
                    Case Is <= 8752
                        Return 23
                    Case Is <= 9392
                        Return 24
                    Case Is <= 10208
                        Return 25
                    Case Is <= 10960
                        Return 26
                    Case Is <= 11744
                        Return 27
                    Case Is <= 12248
                        Return 28
                    Case Is <= 13048
                        Return 29
                    Case Is <= 13880
                        Return 30
                    Case Is <= 14744
                        Return 31
                    Case Is <= 15640
                        Return 32
                    Case Is <= 16568
                        Return 33
                    Case Is <= 17528
                        Return 34
                    Case Is <= 18448
                        Return 35
                    Case Is <= 19472
                        Return 36
                    Case Is <= 20528
                        Return 37
                    Case Is <= 21616
                        Return 38
                    Case Is <= 22496
                        Return 39
                    Case Is <= 23648
                        Return 40
                    Case Else
                        Return -1
                End Select

                'Corrección de errores del 15%
            Case QRCodeEncoder.ERROR_CORRECTION.M
                Select Case ibits
                    Case Is <= 128
                        Return 1
                    Case Is <= 224
                        Return 2
                    Case Is <= 352
                        Return 3
                    Case Is <= 512
                        Return 4
                    Case Is <= 688
                        Return 5
                    Case Is <= 864
                        Return 6
                    Case Is <= 992
                        Return 7
                    Case Is <= 1232
                        Return 8
                    Case Is <= 1456
                        Return 9
                    Case Is <= 1728
                        Return 10
                    Case Is <= 2032
                        Return 11
                    Case Is <= 2320
                        Return 12
                    Case Is <= 2672
                        Return 13
                    Case Is <= 2920
                        Return 14
                    Case Is <= 3320
                        Return 15
                    Case Is <= 3624
                        Return 16
                    Case Is <= 4056
                        Return 17
                    Case Is <= 4504
                        Return 18
                    Case Is <= 5016
                        Return 19
                    Case Is <= 5352
                        Return 20
                    Case Is <= 5712
                        Return 21
                    Case Is <= 6256
                        Return 22
                    Case Is <= 6880
                        Return 23
                    Case Is <= 7312
                        Return 24
                    Case Is <= 8000
                        Return 25
                    Case Is <= 8496
                        Return 26
                    Case Is <= 9024
                        Return 27
                    Case Is <= 9544
                        Return 28
                    Case Is <= 10136
                        Return 29
                    Case Is <= 10984
                        Return 30
                    Case Is <= 11640
                        Return 31
                    Case Is <= 12328
                        Return 32
                    Case Is <= 13048
                        Return 33
                    Case Is <= 13800
                        Return 34
                    Case Is <= 14496
                        Return 35
                    Case Is <= 15312
                        Return 36
                    Case Is <= 15936
                        Return 37
                    Case Is <= 16816
                        Return 38
                    Case Is <= 17728
                        Return 39
                    Case Is <= 18672
                        Return 40
                    Case Else
                        Return -1
                End Select

                'Corrección de errores del 25%
            Case QRCodeEncoder.ERROR_CORRECTION.Q
                Select Case ibits
                    Case Is <= 104
                        Return 1
                    Case Is <= 176
                        Return 2
                    Case Is <= 272
                        Return 3
                    Case Is <= 384
                        Return 4
                    Case Is <= 496
                        Return 5
                    Case Is <= 608
                        Return 6
                    Case Is <= 704
                        Return 7
                    Case Is <= 880
                        Return 8
                    Case Is <= 1056
                        Return 9
                    Case Is <= 1232
                        Return 10
                    Case Is <= 1440
                        Return 11
                    Case Is <= 1648
                        Return 12
                    Case Is <= 1952
                        Return 13
                    Case Is <= 2088
                        Return 14
                    Case Is <= 2360
                        Return 15
                    Case Is <= 2600
                        Return 16
                    Case Is <= 2936
                        Return 17
                    Case Is <= 3176
                        Return 18
                    Case Is <= 3560
                        Return 19
                    Case Is <= 3880
                        Return 20
                    Case Is <= 4096
                        Return 21
                    Case Is <= 4544
                        Return 22
                    Case Is <= 4912
                        Return 23
                    Case Is <= 5312
                        Return 24
                    Case Is <= 5744
                        Return 25
                    Case Is <= 6032
                        Return 26
                    Case Is <= 6464
                        Return 27
                    Case Is <= 6968
                        Return 28
                    Case Is <= 7288
                        Return 29
                    Case Is <= 7880
                        Return 30
                    Case Is <= 8264
                        Return 31
                    Case Is <= 8920
                        Return 32
                    Case Is <= 9368
                        Return 33
                    Case Is <= 9848
                        Return 34
                    Case Is <= 10288
                        Return 35
                    Case Is <= 10832
                        Return 36
                    Case Is <= 11408
                        Return 37
                    Case Is <= 12016
                        Return 38
                    Case Is <= 12656
                        Return 39
                    Case Is <= 13328
                        Return 40
                    Case Else
                        Return -1
                End Select

                ' Corrección de error del 30%
            Case QRCodeEncoder.ERROR_CORRECTION.H
                Select Case ibits
                    Case Is <= 72
                        Return 1
                    Case Is <= 128
                        Return 2
                    Case Is <= 208
                        Return 3
                    Case Is <= 288
                        Return 4
                    Case Is <= 368
                        Return 5
                    Case Is <= 480
                        Return 6
                    Case Is <= 528
                        Return 7
                    Case Is <= 688
                        Return 8
                    Case Is <= 800
                        Return 9
                    Case Is <= 976
                        Return 10
                    Case Is <= 1120
                        Return 11
                    Case Is <= 1264
                        Return 12
                    Case Is <= 1440
                        Return 13
                    Case Is <= 1576
                        Return 14
                    Case Is <= 1784
                        Return 15
                    Case Is <= 2024
                        Return 16
                    Case Is <= 2264
                        Return 17
                    Case Is <= 2504
                        Return 18
                    Case Is <= 2728
                        Return 19
                    Case Is <= 3080
                        Return 20
                    Case Is <= 3248
                        Return 21
                    Case Is <= 3536
                        Return 22
                    Case Is <= 3712
                        Return 23
                    Case Is <= 4112
                        Return 24
                    Case Is <= 4304
                        Return 25
                    Case Is <= 4768
                        Return 26
                    Case Is <= 5024
                        Return 27
                    Case Is <= 5288
                        Return 28
                    Case Is <= 5608
                        Return 29
                    Case Is <= 5960
                        Return 30
                    Case Is <= 6344
                        Return 31
                    Case Is <= 6760
                        Return 32
                    Case Is <= 7208
                        Return 33
                    Case Is <= 7688
                        Return 34
                    Case Is <= 7888
                        Return 35
                    Case Is <= 8432
                        Return 36
                    Case Is <= 8768
                        Return 37
                    Case Is <= 9136
                        Return 38
                    Case Is <= 9776
                        Return 39
                    Case Is <= 10208
                        Return 40
                    Case Else
                        Return -1
                End Select
        End Select
    End Function

    Private Sub establecerColorFondoQR()
        Dim dlSeleccionColor As New ColorDialog

        dlSeleccionColor.FullOpen = True
        dlSeleccionColor.Color =
            Color.FromArgb(pColorFondo.BackColor.ToArgb)
        If dlSeleccionColor.ShowDialog =
                Windows.Forms.DialogResult.OK Then
            pColorFondo.BackColor = dlSeleccionColor.Color
            colorFondoQR = pColorFondo.BackColor.ToArgb
        End If
        dlSeleccionColor.Dispose()
    End Sub

    Private Sub establecerColorQR()
        Dim dlSeleccionColor As New ColorDialog

        dlSeleccionColor.FullOpen = True
        dlSeleccionColor.Color =
            Color.FromArgb(pColorQR.BackColor.ToArgb)

        If dlSeleccionColor.ShowDialog =
                Windows.Forms.DialogResult.OK Then
            pColorQR.BackColor = dlSeleccionColor.Color
            colorQR = pColorFondo.BackColor.ToArgb
        End If
        dlSeleccionColor.Dispose()
    End Sub

    Private Sub btColorFondo_Click(sender As System.Object,
            e As System.EventArgs) Handles pColorFondo.Click, btColorFondo.Click

        establecerColorFondoQR()
    End Sub

    Private Sub btColorQR_Click(sender As System.Object,
            e As System.EventArgs) Handles pColorQR.Click, btColorQR.Click

        establecerColorQR()
    End Sub

    Private Sub pColorQR_Click(sender As System.Object,
            e As System.EventArgs)
        establecerColorQR()
    End Sub

    Private Sub pColorFondo_Click(sender As System.Object,
            e As System.EventArgs)
        establecerColorFondoQR()
    End Sub

    Private Sub formLeerGenerarCodigosQR_Load(sender As System.Object,
            e As System.EventArgs) Handles MyBase.Load
        lsNivelCorreccion.SelectedIndex = 1
        txtEscalaPixel.Text = 4
        'connect to Phone database
        ProjektConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source = c:\VBDB\Working\Projektme2.accdb;Persist Security Info = False; ;")
        ProjektConnection.Open()
        'establish command object
        ProjektCommand = New OleDbCommand("Select * from Projekt", ProjektConnection)
        'establish data adapter/data table
        ProjektAdapter = New OleDbDataAdapter()
        ProjektAdapter.SelectCommand = ProjektCommand
        ProjektTable = New DataTable()
        ProjektAdapter.Fill(ProjektTable)
        'bind controls to data table
        txtIDProjekt.DataBindings.Add("Text", ProjektTable, "IDProjekt")
        txtCustomer.DataBindings.Add("Text", ProjektTable, "Customer")
        txtModule.DataBindings.Add("Text", ProjektTable, "Module")
        txtProjekt.DataBindings.Add("Text", ProjektTable, "ProjektName")
        txtSerial.DataBindings.Add("Text", ProjektTable, "SerialNo")
        txtOrderType.DataBindings.Add("Text", ProjektTable, "OrderType")
        txtDatum.DataBindings.Add("Text", ProjektTable, "DatumPro")
        txtTotalUhr.DataBindings.Add("Text", ProjektTable, "TotalUhr")
        'establish currency manager
        ProjektManager = DirectCast(Me.BindingContext(ProjektTable), CurrencyManager)

        pColorFondo.BackColor = Color.FromArgb(colorFondoQR)
        pColorQR.BackColor = Color.FromArgb(colorQR)

        tabFormatoQR.SelectedTab = TabProjekt
        Call SetState("View")
        txtProjekt_TextChanged(sender, e)
    End Sub
    Private Sub formLesenGenerierenCodeQR_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MyState = "Edit" Or MyState = "Add" Then
            MessageBox.Show("You must finish the current edit before stopping the application.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
            e.Cancel = True
        Else
            Try
                'save the updated Projekt table
                Dim ProjektAdapterCommands As New OleDbCommandBuilder(ProjektAdapter)
                ProjektAdapter.Update(ProjektTable)
            Catch ex As Exception
                MessageBox.Show("Error saving database to file:" + ControlChars.CrLf + ex.Message, "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
            'close the connection
            ProjektConnection.Close()
            'dispose of the objects
            ProjektConnection.Dispose()
            ProjektCommand.Dispose()
            ProjektAdapter.Dispose()
            ProjektTable.Dispose()
        End If
    End Sub

    Private Sub txtProjekt_TextChanged(sender As System.Object,
            e As System.EventArgs) Handles txtSerial.TextChanged, txtProjekt.TextChanged, txtOrderType.TextChanged, txtModule.TextChanged,
        txtCustomer.TextChanged, txtOrderType.TextChanged, txtProjekt.TextChanged, txtIDProjekt.TextChanged,
        txtModule.TextChanged, txtSerial.TextChanged, txtDatum.TextChanged
        txtTextoQR.Text = "Datum:" + txtDatum.Text + vbCrLf + "ID Projekt:" + txtIDProjekt.Text + vbCrLf + "Customer:" + txtCustomer.Text + vbCrLf + "Order Type:" + txtOrderType.Text + vbCrLf +
        "Projekt:" + txtProjekt.Text + vbCrLf + "Module:" + txtModule.Text + vbCrLf + "Serial:" + txtSerial.Text
    End Sub

    Private Sub txtURL_TextChanged(sender As System.Object,
            e As System.EventArgs) Handles txtURL.TextChanged
        txtTextoQR.Text = txtURL.Text
    End Sub

    Private Sub txtTextoLibre_TextChanged(sender As System.Object,
            e As System.EventArgs) Handles txtTextoLibre.TextChanged
        txtTextoQR.Text = txtTextoLibre.Text
    End Sub

    Private Sub txtEventoAsunto_TextChanged(sender As System.Object,
            e As System.EventArgs) Handles txtEventoUbicacion.TextChanged, txtEventoHastaHora.ValueChanged, txtEventoHasta.ValueChanged, txtEventoDesdeHora.ValueChanged, txtEventoDesde.ValueChanged, txtEventoDescripcion.TextChanged, txtEventoAsunto.TextChanged
        txtTextoQR.Text = "BEGIN:VEVENT" + vbCrLf + "SUMMARY:" +
            txtEventoAsunto.Text + vbCrLf +
            "DTSTART:" +
            txtEventoDesde.Value.Year.ToString("0000") +
            txtEventoDesde.Value.Month.ToString("00") +
            txtEventoDesde.Value.Day.ToString("00") +
            "T" + txtEventoDesdeHora.Value.Hour.ToString("00") +
            txtEventoDesdeHora.Value.Minute.ToString("00") +
            "00" + vbCrLf +
            "DTEND:" + txtEventoHasta.Value.Year.ToString("0000") +
            txtEventoHasta.Value.Month.ToString("00") +
            txtEventoHasta.Value.Day.ToString("00") +
            "T" + txtEventoHastaHora.Value.Hour.ToString("00") +
            txtEventoHastaHora.Value.Minute.ToString("00") +
            "00" + vbCrLf +
            "LOCATION:" + txtEventoUbicacion.Text + vbCrLf +
            "DESCRIPTION:" + txtEventoDescripcion.Text +
            vbCrLf + "END:VEVENT"
    End Sub

    Private Sub btGenerarQR_Click(sender As System.Object,
            e As System.EventArgs) Handles btGenerarQR.Click
        If txtEscalaPixel.Text.Trim = "" Then
            MsgBox("Debe indicar la escala de los píxeles del " +
                   "QR que se generará.",
                   MsgBoxStyle.Information + MsgBoxStyle.OkOnly)
            txtEscalaPixel.Focus()
        Else

            If Val(txtEscalaPixel.Text) = 0 Then
                txtEscalaPixel.Text = 4
            End If

            Dim generarCodigoQR As QRCodeEncoder = New QRCodeEncoder
            generarCodigoQR.QRCodeEncodeMode =
                Codec.QRCodeEncoder.ENCODE_MODE.BYTE
            generarCodigoQR.QRCodeScale = Int32.Parse(txtEscalaPixel.Text)

            Select Case lsNivelCorreccion.Text
                Case "Bajo (7%)"
                    generarCodigoQR.QRCodeErrorCorrect =
                        Codec.QRCodeEncoder.ERROR_CORRECTION.L
                Case "Medio (15%)"
                    generarCodigoQR.QRCodeErrorCorrect =
                        Codec.QRCodeEncoder.ERROR_CORRECTION.M
                Case "Alto (25%)"
                    generarCodigoQR.QRCodeErrorCorrect =
                        Codec.QRCodeEncoder.ERROR_CORRECTION.Q
                Case "Muy alto (30%)"
                    generarCodigoQR.QRCodeErrorCorrect =
                        Codec.QRCodeEncoder.ERROR_CORRECTION.H
            End Select

            'La versión "0" calcula automáticamente el tamaño
            generarCodigoQR.QRCodeVersion = 0

            '' --------- Forzar una determinada version -----------
            ''En caso de querer forzar una determinada version 
            '(tamaño) el siguiente código devuelve la
            ''versión mínima para el texto que se quiere códificar:
            'Dim iVersion As Integer = 
            '    AdjustQRVersion(TextBox1.Text, QRCodeEncoder.QRCodeErrorCorrect)
            'If iVersion = -1 Then
            '    MessageBox.Show("El texto es demasiado grande o el " +
            '        "Correction Level (ERROR_CORRECTION) no es el apropiado")
            '    Exit Sub
            'Else
            '    qrCodeEncoder.QRCodeVersion = iVersion
            'End If
            '' -----------------------------------------------------

            generarCodigoQR.QRCodeBackgroundColor =
                System.Drawing.Color.FromArgb(colorFondoQR)
            generarCodigoQR.QRCodeForegroundColor =
                System.Drawing.Color.FromArgb(colorQR)

            Try
                If opForzarUTF.Checked Then
                    'Con UTF-8 podremos añadir caracteres como ñ, tildes, etc.
                    imgQR.Image = generarCodigoQR.Encode(txtTextoQR.Text,
                                    System.Text.Encoding.UTF8)

                Else
                    imgQR.Image = generarCodigoQR.Encode(txtTextoQR.Text)

                End If
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
            End Try
        End If
    End Sub


    Private Sub btGuardarQR_Click(sender As System.Object,
            e As System.EventArgs) Handles btGuardarQR.Click
        Dim dlGuardar As New SaveFileDialog

        If IsNothing(imgQR.Image) Then
            MsgBox("No se ha generado el Código QR. Antes de guardar " +
                   "debe generar un código QR.",
                   MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
        Else
            dlGuardar.Filter = "JPEG|*.jpg|Mapa de Bits|*.bmp|Gif|*.gif|PNG|*.png"
            dlGuardar.Title = "Guardar código QR"
            dlGuardar.FileName = "codigo_qr"
            dlGuardar.ShowDialog()
            If (dlGuardar.FileName <> "") Then
                Select Case dlGuardar.FilterIndex
                    Case 1
                        imgQR.Image.Save(dlGuardar.FileName,
                            System.Drawing.Imaging.ImageFormat.Jpeg)
                    Case 2
                        imgQR.Image.Save(dlGuardar.FileName,
                            System.Drawing.Imaging.ImageFormat.Bmp)
                    Case 3
                        imgQR.Image.Save(dlGuardar.FileName,
                            System.Drawing.Imaging.ImageFormat.Gif)
                    Case 4
                        imgQR.Image.Save(dlGuardar.FileName,
                            System.Drawing.Imaging.ImageFormat.Png)
                End Select
            End If
        End If
    End Sub

    Private Sub btImprimirQR_Click(sender As System.Object,
                e As System.EventArgs) Handles btImprimirQR.Click
        If IsNothing(imgQR.Image) Then
            MsgBox("No se ha generado el Código QR. Antes de " +
                   "imprimir debe generar un código QR.",
                   MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
        Else
            dlImprimir.Document = imprimirDoc
            If dlImprimir.ShowDialog = Windows.Forms.DialogResult.OK Then
                imprimirDoc.Print()
            End If
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(sender As System.Object,
          e As System.Drawing.Printing.PrintPageEventArgs) Handles imprimirDoc.PrintPage

        e.Graphics.DrawImage(imgQR.Image, 0, 0)
    End Sub

    Private Sub btSelImagenQR_Click(sender As System.Object,
            e As System.EventArgs) Handles btSelImagenQR.Click
        Dim dlAbrirImagenQR As New OpenFileDialog

        dlAbrirImagenQR.InitialDirectory =
            My.Computer.FileSystem.SpecialDirectories.MyPictures
        dlAbrirImagenQR.Filter = "*.bmp;*.gif;*.jpg;*.png|*.bmp;*.gif;*.jpg;*.png"
        dlAbrirImagenQR.RestoreDirectory = True
        dlAbrirImagenQR.CheckFileExists = True
        dlAbrirImagenQR.CheckPathExists = True
        If dlAbrirImagenQR.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtLeerQRImagen.Text = dlAbrirImagenQR.FileName
        End If
    End Sub

    Private Sub btDecodificarImagenQR_Click(sender As System.Object,
            e As System.EventArgs) Handles btDecodificarImagenQR.Click
        If IsNothing(imgQRExistente.Image) Then
            MsgBox("Antes de decodificar una imagen QR a su texto " +
                   "correspondiente QR Code debe seleccionar la imagen.",
                   MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly)
            btSelImagenQR.Focus()
        Else
            Try
                Dim decoder As QRCodeDecoder = New QRCodeDecoder
                If opLeerForzarUTF8.Checked Then
                    txtLeerQRTexto.Text =
                        decoder.decode(New QRCodeBitmapImage(
                                       New Bitmap(imgQRExistente.Image)),
                                   System.Text.Encoding.UTF8)
                Else
                    txtLeerQRTexto.Text =
                        decoder.decode(New QRCodeBitmapImage(
                                       New Bitmap(imgQRExistente.Image)))
                End If
            Catch ex As Exception
                MsgBox(ex.Message,
                       MsgBoxStyle.Critical + MsgBoxStyle.OkOnly)
            End Try
        End If
    End Sub

    Private Sub txtLeerQRImagen_TextChanged(sender As System.Object,
            e As System.EventArgs) Handles txtLeerQRImagen.TextChanged
        If System.IO.File.Exists(txtLeerQRImagen.Text) Then
            imgQRExistente.Image = New Bitmap(txtLeerQRImagen.Text)
        End If
    End Sub

    Private Sub tabFormatoQR_SelectedIndexChanged(sender As System.Object,
               e As System.EventArgs) Handles tabFormatoQR.SelectedIndexChanged
        If tabFormatoQR.SelectedTab Is tabQRURL Then
            txtURL_TextChanged(sender, e)
        End If

        If tabFormatoQR.SelectedTab Is tabQRTexto Then
            txtTextoLibre_TextChanged(sender, e)
        End If

        If tabFormatoQR.SelectedTab Is tabQREvento Then
            txtEventoAsunto_TextChanged(sender, e)
        End If

        If tabFormatoQR.SelectedTab Is TabProjekt Then
            txtProjekt_TextChanged(sender, e)
        End If
    End Sub

    Private Sub linkURL_LinkClicked(sender As System.Object,
                e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles linkURL.LinkClicked

        System.Diagnostics.Process.Start("http://www.me2-gmbh.org")
    End Sub



    Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        ProjektManager.Position -= 1
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
        ProjektManager.Position += 1
    End Sub

    Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
        ProjektManager.Position = 0
    End Sub

    Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
        ProjektManager.Position = ProjektManager.Count - 1
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim Response As Windows.Forms.DialogResult
        Response = MessageBox.Show("Are you sure you want to delete this record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If Response = Windows.Forms.DialogResult.No Then
            Exit Sub
        End If
        Try
            ProjektManager.RemoveAt(ProjektManager.Position)
        Catch ex As Exception
            MessageBox.Show("Error deleting record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub SetState(ByVal AppState As String)
        MyState = AppState
        Select Case AppState
            Case "View"
                btnFirst.Enabled = True
                btnPrevious.Enabled = True
                btnNext.Enabled = True
                btnLast.Enabled = True
                btnEdit.Enabled = True
                btnAddNew.Enabled = True
                btnSave.Enabled = False
                btnCancel.Enabled = False
                'txtIDProjekt.TabStop = True
                txtSerial.BackColor = Color.White
                txtSerial.ForeColor = Color.Black
                txtCustomer.ReadOnly = True
                txtProjekt.ReadOnly = True
                txtSerial.ReadOnly = True
                txtModule.ReadOnly = True
                txtOrderType.ReadOnly = True
                txtDatum.TabStop = True
            Case "Add", "Edit"
                btnFirst.Enabled = False
                btnPrevious.Enabled = False
                btnNext.Enabled = False
                btnLast.Enabled = False
                btnEdit.Enabled = False
                btnSave.Enabled = True
                btnCancel.Enabled = True
                If MyState = "Edit" Then
                    btnAddNew.Enabled = False
                    txtIDProjekt.TabStop = False
                    txtSerial.BackColor = Color.Red
                    txtSerial.ForeColor = Color.White
                Else
                    txtIDProjekt.TabStop = True
                End If
                txtCustomer.ReadOnly = False
                txtProjekt.ReadOnly = False
                txtSerial.ReadOnly = False
                txtModule.ReadOnly = False
                txtOrderType.ReadOnly = False
        End Select
        txtCustomer.Focus()
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        Call SetState("Edit")
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not (ValidateData()) Then Exit Sub
        Dim SavedProjekt As String = txtProjekt.Text
        Dim SavedRow As Integer
        Try
            ProjektManager.EndCurrentEdit()
            ProjektTable.DefaultView.Sort = "Customer"
            SavedRow = ProjektTable.DefaultView.Find(SavedProjekt)
            ProjektManager.Position = SavedRow
            MessageBox.Show("Record saved.", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Call SetState("View")
        Catch ex As Exception
            MessageBox.Show("Error savid record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ProjektManager.CancelCurrentEdit()
        If MyState = "Add" Then
            ProjektManager.Position = MyBookmark
        End If
        Call SetState("View")
    End Sub
    Private Sub btnAddNew_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
        Try
            MyBookmark = ProjektManager.Position
            ProjektManager.AddNew()
            Call SetState("Add")
        Catch ex As Exception
            MessageBox.Show("Error adding record.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Private Sub btnDone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDone.Click
        Me.Close()
    End Sub

    Private Function ValidateData() As Boolean
        Dim Message As String = ""
        Dim ALLOK As Boolean = True
        'Check for name
        If txtCustomer.Text.Trim = "" Then
            Message = "You must enter an Customer Name." + ControlChars.CrLf
            txtCustomer.Focus()
            ALLOK = False
        End If
        If Not (ALLOK) Then
            MessageBox.Show(Message, "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
        Return (ALLOK)
    End Function
End Class