Public Class frmLKWDetails
    Public kundenNr = -1
    Public KfzKennzeichen = ""
    Public oldkfz = ""

    Public addnew = False
    Public WithEvents s As New cBinding("SDL") 'fürs Binding
    Dim changed As Boolean = False
    Dim ready As Boolean = False

    Sub New(kundenNr, KfzKennzeichen, Optional addnew = False)
        
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        Me.kundenNr = kundenNr
        Me.KfzKennzeichen = KfzKennzeichen
        Me.addnew = addnew

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.

    End Sub
    Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
        txtKennzeichen.Text = txtKennzeichen.Text.ToUpper.Trim
        txtNationalitaet.Text = txtNationalitaet.Text.ToUpper
        Label5.ForeColor = Color.Black
        Label1.ForeColor = Color.Black
        If KdSearchBox1.KdNr < 0 Then Label5.ForeColor = Color.Red : Me.DialogResult = Windows.Forms.DialogResult.None : Exit Sub
        If txtKennzeichen.Text = "" Then Label1.ForeColor = Color.Red : Me.DialogResult = Windows.Forms.DialogResult.None : Exit Sub

        If s.updateBinding Then
            Dim SQL As New SQL
            If oldkfz <> txtKennzeichen.Text Then
                VERAG_PROG_ALLGEMEIN.DATENVERVER_OPTIONS.REMANE_FOLDER_KDNR(New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, oldkfz, Nothing, Nothing),
                                                                            New VERAG_PROG_ALLGEMEIN.cDatenserver_Change_Value("DOKUMENTE", "MDM", kundenNr, txtKennzeichen.Text, Nothing, Nothing))
                SQL.doSQL("UPDATE SDL SET KfzKennzeichen= '" & txtKennzeichen.Text & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & oldkfz & "'", "SDL", True)
                SQL.doSQL("UPDATE [Kartenpool] SET KfzKennzeichen= '" & txtKennzeichen.Text & "' WHERE KundenNr='" & kundenNr & "' AND  KfzKennzeichen='" & oldkfz & "'", "SDL", True)
            End If
            Me.Close()
        End If
    End Sub


    Sub initDataBinding()
        Try
            Dim tmpHist As String = "" 'If History <> "" Then tmpHist = " AND History='" & History & "'"
            s.initBinding("SELECT * FROM [LKW] WHERE KundenNr='" & kundenNr & "' AND KfzKennzeichen='" & KfzKennzeichen & "' ", "SDL")

            AddHandler s.bindingdataTable.ColumnChanged, AddressOf BindingTableColumnChanged
            ' AddHandler s.bindingdataTable.TableNewRow, Sub(snd As Object, ev As DataTableNewRowEventArgs)
            'Dim row As DataRow = ev.Row
            ' row("KundenNr") = kundenNr
            ' row("Erfassungsdatum") = Now
            '  row("Sachbearbeiter") = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            '  s.bindingSource.MoveLast()
            ' End Sub

            KdSearchBox1.DataBindings.Add(New Binding("KdNr", s.bindingSource, "KundenNr", True, DataSourceUpdateMode.OnPropertyChanged, ""))

            txtKennzeichen.DataBindings.Add(New Binding("Text", s.bindingSource, "KfzKennzeichen", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            txtNationalitaet.DataBindings.Add(New Binding("Text", s.bindingSource, "Nationalität", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            cbxVerkauft.DataBindings.Add(New Binding("Checked", s.bindingSource, "Verkauft", True, DataSourceUpdateMode.OnPropertyChanged, False))
            cbxKZAenderung.DataBindings.Add(New Binding("Checked", s.bindingSource, "KZAenderung", True, DataSourceUpdateMode.OnPropertyChanged, False))
            cbxAbgemeldet.DataBindings.Add(New Binding("Checked", s.bindingSource, "Abgemeldet", True, DataSourceUpdateMode.OnPropertyChanged, False))
            txtVermerk.DataBindings.Add(New Binding("Text", s.bindingSource, "Vermerk", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblErfassung.DataBindings.Add(New Binding("Text", s.bindingSource, "Erfassungsdatum", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblAenderung.DataBindings.Add(New Binding("Text", s.bindingSource, "Änderungsdatum", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            lblSachbearb.DataBindings.Add(New Binding("Text", s.bindingSource, "Sachbearbeiter", True, DataSourceUpdateMode.OnPropertyChanged, ""))


            txtBaujahr.DataBindings.Add(New Binding("Text", s.bindingSource, "Baujahr", True, DataSourceUpdateMode.OnPropertyChanged, ""))
            cbxMiete.DataBindings.Add(New Binding("Checked", s.bindingSource, "KzMiete", True, DataSourceUpdateMode.OnPropertyChanged, False))
            cbxLeasing.DataBindings.Add(New Binding("Checked", s.bindingSource, "KzLeasing", True, DataSourceUpdateMode.OnPropertyChanged, False))
            cbxFinanzierungBank.DataBindings.Add(New Binding("Checked", s.bindingSource, "KzFinanzierungBank", True, DataSourceUpdateMode.OnPropertyChanged, False))

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


        If addnew Then newEntry() 'NEUER EINTRAG!
        ready = True
    End Sub

    Public Sub newEntry()
        '  s.updateBinding()
        Dim row = s.bindingdataTable.NewRow()
        row.Item("KundenNr") = kundenNr
        row.Item("KfzKennzeichen") = ""
        s.bindingdataTable.Rows.Add(row)

        s.bindingSource.MoveLast()
        'If save Then updateBinding()
        lblErfassung.Text = Now.ToString("dd.MM.yyyy HH:mm")
        lblSachbearb.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
        If kundenNr < 0 Then KdSearchBox1.Enabled = True
    End Sub

    Sub BindingTableColumnChanged(sender As Object, e As System.Data.DataColumnChangeEventArgs)
        If ready Then
            lblSachbearb.Text = VERAG_PROG_ALLGEMEIN.cAllgemein.USRKURZNAME
            lblAenderung.Text = Now.ToString("dd.MM.yyyy HH:mm")
            changed = True
        End If
    End Sub


    Private Sub btnAbbrechen_Click(sender As Object, e As EventArgs) Handles btnAbbrechen.Click ', Me.FormClosing
        If changed Then
            Select Case MsgBox("Änderungen speichern", vbYesNoCancel)
                Case vbYes : Me.DialogResult = Windows.Forms.DialogResult.None : btnOK.PerformClick()
                Case vbNo : Me.Close()
                Case vbCancel : Me.DialogResult = Windows.Forms.DialogResult.None
            End Select
        Else
            Me.Close()
        End If
    End Sub

    Private Sub frmLKWDetails_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Return Then
            btnOK.PerformClick()
        End If
    End Sub

    Private Sub frmLKWDetails_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not VERAG_PROG_ALLGEMEIN.cBerechtignunen.CHECK_BERECHTIGUNG_bool("MDM_LKW_bearbeiten", Me) Then
            MsgBox("Keine Berechtigung!")
            Me.Close()
        End If
        KdSearchBox1.initKdBox(Me)
        initDataBinding()
        oldkfz = KfzKennzeichen
    End Sub

    Private Sub txtKennzeichen_LostFocus(sender As Object, e As EventArgs) Handles txtKennzeichen.LostFocus
        txtKennzeichen.Text = txtKennzeichen.Text.ToUpper
    End Sub
    Private Sub txtNationalitaet_LostFocus(sender As Object, e As EventArgs) Handles txtNationalitaet.LostFocus
        txtNationalitaet.Text = txtNationalitaet.Text.ToUpper
    End Sub

    Private Sub cbxVerkauft_CheckedChanged(sender As Object, e As EventArgs) Handles cbxVerkauft.CheckedChanged, cbxKZAenderung.CheckedChanged
        sender.backcolor = IIf(sender.checked, Color.Red, Color.White)
    End Sub

End Class