Imports System.Reflection
Imports System.ComponentModel


Public Class frmCardDetails
    Public addnew = False
    Public SDLLeistung As String = ""
    Public history As String = ""
    Public kdnr As String = ""
    Public KfzKennzeichen As String = ""
    Public newData = False
    ' Dim valueChanged As Boolean = False
    Dim kundenSQL As New kundenSQL
    ' Dim usrCntlBinding As cBinding = Nothing

    Event SAVE()

    Function checksave() As Boolean
        If bndSave.Enabled Then
            Select Case MsgBox("Änderungen speichern?", vbYesNoCancel)
                Case vbYes : saveMe()
                Case vbCancel : Me.DialogResult = Windows.Forms.DialogResult.None : Return False
            End Select
        End If
        Return True
    End Function

    Sub saveMe()
        RaiseEvent SAVE()
    End Sub
    Sub init()

        '    If checksave() = False Then Exit Sub

        '   valueChanged = False
        bndSave.Enabled = False

        pnlSDLLeistungenDetails.Controls.Clear()

        If SDLLeistung = 100 Then ' IDS
            Dim usrcntl As New usrCntlSDLLeistungenIDS(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 101 Then ' WÖLFL
            Dim usrcntl As New usrCntlSDLLeistungenWOELFL(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 200 Then ' AT-MAUT
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungMautAT(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 201 Then ' DE-MAUT
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungMautDE(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 202 Then ' IT-MAUT
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenIT(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 203 Then ' CZ MAUT
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenCZ(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 204 Then ' FR-MAUT
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenFR(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 205 Then ' MotorWayMaut
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenMotorWayMaut(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 206 Then ' DUOBOX FR / ES
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenDUOBOX(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 207 Then ' FREJUS
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenFREJUS(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 208 Then ' MSE Card
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenMSE(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 209 Then ' PL MAUT
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenPL(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)

        ElseIf SDLLeistung = 210 Then ' SI-MAUT
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenSI(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 211 Then ' ECOTAX
            '  Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenECOTAX(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 212 Then ' UTA
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungenUTA(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 213 Then ' HUGO
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungHUGO(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 214 Then ' plose
            ' Panel2.BackgroundImage = Nothing
            Dim usrcntl As New usrCntlSDLLeistungPLOSE(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        ElseIf SDLLeistung = 500 Then ' MAN
            Dim usrcntl As New usrCntlSDLLeistungenMANAbgas(kdnr, KfzKennzeichen, SDLLeistung, history)
            If addnew Then usrcntl.addnew = True : bndSave.Enabled = True 'NEUER EINTRAG!
            pnlSDLLeistungenDetails.Controls.Add(usrcntl)

            RemoveClickEvent(btnSave)
            AddHandler btnSave.Click, Sub()
                                          If usrcntl.save Then Me.DialogResult = Windows.Forms.DialogResult.OK : Me.Close()
                                      End Sub
            AddHandler Me.SAVE, Sub()
                                    If usrcntl.save Then bndSave.Enabled = False
                                End Sub

            initChangeEvent(usrcntl)
        End If

        ' RaiseEvent SAVE()


        'If addnew Then bndSave.Enabled = True
        'If Not usrCntlBinding Is Nothing Then
        'AddHandler usrCntlBinding.bindingdataTable.ColumnChanged, Sub()
        '    setChange()
        ' End Sub
        '  AddHandler usrCntlBinding.bindingdataTable.RowDeleted, Sub()
        '     setChange()
        'End Sub
        '    End If

    End Sub

    Private Sub RemoveClickEvent(b As Button)
        Dim f1 As FieldInfo = GetType(Control).GetField("EventClick", BindingFlags.Static Or BindingFlags.NonPublic)
        Dim obj As Object = f1.GetValue(b)
        Dim pi As PropertyInfo = b.GetType().GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim list As EventHandlerList = DirectCast(pi.GetValue(b, Nothing), EventHandlerList)
        list.RemoveHandler(obj, list(obj))
    End Sub
    Private Sub RemoveClickEvent(b As ToolStripButton)
        Dim f1 As FieldInfo = GetType(Control).GetField("EventClick", BindingFlags.Static Or BindingFlags.NonPublic)
        Dim obj As Object = f1.GetValue(b)
        Dim pi As PropertyInfo = b.GetType().GetProperty("Events", BindingFlags.NonPublic Or BindingFlags.Instance)
        Dim list As EventHandlerList = DirectCast(pi.GetValue(b, Nothing), EventHandlerList)
        list.RemoveHandler(obj, list(obj))
    End Sub

    Private Sub frmDatenDetails_Load(sender As Object, e As EventArgs) Handles Me.Load
        init()

        Dim kundenSQL As New kundenSQL
        Dim cKunden As cKunden = kundenSQL.getKundeByKdNr(kdnr)
        lblFirma.Text = cKunden.Kurzname
        If cKunden.Abfertigungsverbot Then
            lblFirma.ForeColor = Color.Red
            lblFirma.Text &= " (Abfertigungsverbot)"
        Else
            lblFirma.ForeColor = Color.Black
        End If

    End Sub




    'Läuft alle Elememte durch -> Wenn Änderung, dann wird der Button aktiv
    Public Sub initChangeEvent(ByVal oContainer As Object)
        Exit Sub
        If Not IsNothing(oContainer) Then
            ' alle Controls des Container-Objekts durchlaufen
            For Each oControl As Control In oContainer.Controls
                ' Falls es sich um ein Container-Control handelt und die 
                ' TextBox-Controls ebenfalls zurückgesetzt werden sollen, muss
                ' die Funktion ResetTextBox rekursiv erneut aufgerufen werden
                If oControl.Controls.Count > 0 Then
                    initChangeEvent(oControl)
                Else
                    ' Sofern es sich um ein TextBox-Control handelt...
                    If TypeOf oControl Is TextBox Or TypeOf oControl Is RichTextBox Then
                        AddHandler oControl.TextChanged, AddressOf setChange
                    End If
                    If TypeOf oControl Is CheckBox Then
                        AddHandler DirectCast(oControl, CheckBox).CheckStateChanged, AddressOf setChange
                    End If
                    If TypeOf oControl Is DateTimePicker Then
                        AddHandler DirectCast(oControl, DateTimePicker).ValueChanged, AddressOf setChange
                    End If
                    If TypeOf oControl Is ComboBox Then
                        AddHandler DirectCast(oControl, ComboBox).SelectedIndexChanged, AddressOf setChange
                    End If
                    If TypeOf oControl Is RadioButton Then
                        AddHandler DirectCast(oControl, RadioButton).CheckedChanged, AddressOf setChange
                    End If

                End If
            Next
        End If
    End Sub

    Private Sub setChange()
        'valueChanged = True
        btnSave.Enabled = True
        bndSave.Enabled = True
    End Sub


    Private Sub cboLeistungen_KeyUp(sender As Object, e As KeyEventArgs)
        If e.KeyCode = Keys.Return Then
            init()
        End If
    End Sub


    Private Sub ComboBox2_SelectedValueChanged(sender As Object, e As EventArgs)
        ' If pnlSDLLeistungenDetails.Controls.Count = 0 And (cboLeistungen.Text <> "" And cboLKW.Text <> "" And IsNumeric(kdnr) AndAlso kdnr > 0) Then
        'init()
        'End If
    End Sub



    Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
        's.row_kdNr = kdnr
        'FlatButton1.PerformClick()
    End Sub




    Private Sub FlatButton1_Click(sender As Object, e As EventArgs)
        Me.Close()
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '    btnSave.PerformClick()
        Me.Close()
        Me.Hide()
        cProgramFunctions.newFrmData(Me.FindForm, kdnr, New Integer = -1, KfzKennzeichen, New Integer = -1)
        Exit Sub

        newData = True
        btnSave.PerformClick()
        Exit Sub
        saveMe()
        If bndSave.Enabled = False Then

            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()

            '  cProgramFunctions.newFrmData(Me.ParentForm)
        End If
    End Sub


    Private Sub btnNEU_Click(sender As Object, e As EventArgs)
        init()
    End Sub

    Private Sub FlatButton1_Click_1(sender As Object, e As EventArgs) Handles FlatButton1.Click
        If checksave() Then
            Me.Close()
        End If
    End Sub



    Private Sub btnAbbrechen_Click(sender As Object, e As EventArgs) Handles btnAbbrechen.Click
        If bndSave.Enabled Then
            If checksave() Then Exit Sub
        End If
    End Sub

    Private Sub bndSave_Click(sender As Object, e As EventArgs) Handles bndSave.Click
        saveMe()
    End Sub





End Class