Public Class usrcntlNewDataLKW

    Event LKW_ADDED(Kfz)
    Event LKW_DEL(Kfz)
    Public KdNr As String = -1
    Dim kundenSQL As New kundenSQL
    Sub New(KdNr)
        InitializeComponent()
        Me.KdNr = KdNr

    End Sub


    '  Private Sub cboLKW_DropDown(sender As Object, e As KeyEventArgs) Handles cboLKW.KeyDown
    '     If e.KeyCode = Keys.Return Then
    'Dim sql As New SQL
    '         Try
    '   Dim kdtmp = SQL.getValueTxtBySql("SELECT TOP 1 isnull([KundenNr],-1)  FROM [SDL].[dbo].[SDL] WHERE KfzKennzeichen ='" & cboLKW.Text & "' ORDER BY Erfassungsdatum DESC")
    '              KdNr = kdtmp '??????
    '           Catch ex As Exception
    '          End Try
    '      End If
    '  End Sub

    Private Sub cboLKW_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboLKW.SelectedIndexChanged
        Label5.Visible = False
        Label6.Visible = False
        lblKZAenderung.Visible = False
        If IsNumeric(KdNr) AndAlso KdNr > 0 Then
            Try
                Dim dt As DataTable = kundenSQL.loadDgvBySql("SELECT isnull([Verkauft],0) as Verkauft,  isnull([Abgemeldet],0) as Abgemeldet ,isnull(KZAenderung,0) as KZAenderung FROM [LKW] WHERE [KundenNr]='" & KdNr & "' AND [KfzKennzeichen]='" & cboLKW.Text & "' ")
                If dt.Rows.Count > 0 Then
                    Label5.Visible = dt(0)("Verkauft")
                    Label6.Visible = dt(0)("Abgemeldet")
                    lblKZAenderung.Visible = dt(0)("KZAenderung")

                End If
            Catch ex As Exception
            End Try
        End If
        If DirectCast(cboLKW.SelectedItem, VERAG_PROG_ALLGEMEIN.MyListItem).Value <> "" Then
            btnAddLKW.Visible = False
        Else
            btnAddLKW.Visible = True
        End If
        FlatButton1.Visible = Not btnAddLKW.Visible
    End Sub

    Private Sub cboLKW_TextChanged(sender As Object, e As EventArgs) Handles cboLKW.TextChanged
        Label5.Visible = False
        Label6.Visible = False
    End Sub

    Private Sub FlatButton2_Click(sender As Object, e As EventArgs) Handles btnAddLKW.Click
        If IsNumeric(KdNr) AndAlso KdNr > 0 Then
            Dim f As New frmAddLKW
            If f.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                If f.txtKennzeichen.Text <> "" Then
                    kundenSQL.insertLKW(KdNr, f.txtKennzeichen.Text, f.MyTextBox2.Text)
                    'initCboLkw(f.txtKennzeichen.Text)
                    'RaiseEvent LKW_ADDED(f.txtKennzeichen.Text)
                    initCBO()
                    For Each i In cboLKW.Items
                        If DirectCast(i, VERAG_PROG_ALLGEMEIN.MyListItem).Value = f.txtKennzeichen.Text Then
                            cboLKW.SelectedItem = i
                            Exit For
                        End If
                    Next
                Else
                    MsgBox("Kein LKW-Kz angegeben!")
                End If
            End If
        Else
            MsgBox("Kein Kunde ausgewählt!")
        End If
    End Sub

    Sub initCBO()
        cboLKW.fillWithSQL("SELECT KfzKennzeichen,KfzKennzeichen FROM LKW WHERE [KundenNr]='" & KdNr & "' ORDER BY LKW.KfzKennzeichen", False, "SDL", True)
    End Sub

    Private Sub FlatButton1_Click(sender As Object, e As EventArgs) Handles FlatButton1.Click
        If cboLKW._value <> "" Then RaiseEvent LKW_DEL(cboLKW._value)
    End Sub

    Private Sub usrcntlNewDataLKW_Load(sender As Object, e As EventArgs) Handles Me.Load
        initCBO()
    End Sub
End Class
