Public Class usrcntlVollmacht_FA
    Implements FormualrInterface

    Public Event CHANGED(name, value)
    Public Event KDNR_CHANGED(kdnr)
    Public Event DELETE(c As Control)
    Public Event MOVE_UP(c As Control)
    Public Event MOVE_DOWN(c As Control)

    Public KdNR As Integer = -1
    Public Sub ChangeKDNr(kdnr As Integer) _
       Implements FormualrInterface.ChangeKDNr
        initKdNR(kdnr)
    End Sub

    'Dim KdNR As Integer = -1
    Sub initControlsHandler()
        For Each c In Me.Controls
            Select Case c.GetType.ToString
                Case GetType(VERAG_PROG_ALLGEMEIN.MyTextBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyTextBox).Leave, Sub()
                                                                                        RaiseEvent CHANGED(c.name, c.text)
                                                                                    End Sub
                Case GetType(VERAG_PROG_ALLGEMEIN.MyComboBox).ToString
                    AddHandler DirectCast(c, VERAG_PROG_ALLGEMEIN.MyComboBox).SelectedIndexChanged, Sub()
                                                                                                        RaiseEvent CHANGED(c.name, c._value)
                                                                                                    End Sub
                Case GetType(TextBox).ToString
                    AddHandler DirectCast(c, TextBox).Leave, Sub()
                                                                 RaiseEvent CHANGED(c.name, c.text)
                                                             End Sub
            End Select
        Next
    End Sub


    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        Dim kdsrch As New SDL.frmKundenSuche
        'Panel1.Enabled = False
        If kdsrch.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK Then
            If IsNumeric(kdsrch.kundenNrSearch.Text) Then
                initKdNR(kdsrch.kundenNrSearch.Text)
                RaiseEvent KDNR_CHANGED(KdNR)
            End If
        End If
        '  Panel1.Enabled = True
    End Sub

    Public Sub initKdNR(KdNR)
        Me.KdNR = KdNR
        Dim KUNDE As New VERAG_PROG_ALLGEMEIN.cKunde(KdNR)
        Dim ADRESSE As New VERAG_PROG_ALLGEMEIN.cAdressen(KdNR)

        txtFirma.Text = If(ADRESSE.Name_1, "") & If(If(ADRESSE.Name_2, "") <> "", " " & ADRESSE.Name_2, "")
        txtAdresse1.Text = If(ADRESSE.Straße, "")
        txtAdresse2.Text = (If(ADRESSE.LandKz, "") & " " & If(ADRESSE.PLZ, "") & " " & If(ADRESSE.Ort, "")).ToString.Trim
        txtSteuerNr.Text = If(KUNDE.EORITIN, "")
        txtEmail.Text = If(ADRESSE.E_Mail, "")
        txtSteuerNr.Text = If(ADRESSE.Steuernummer, "")

    End Sub


    Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click
        RaiseEvent DELETE(Me)
    End Sub

    Private Sub btnup_Click(sender As Object, e As EventArgs) Handles btnUp.Click
        RaiseEvent MOVE_UP(Me)
    End Sub

    Private Sub btnDown_Click(sender As Object, e As EventArgs) Handles btnDown.Click
        RaiseEvent MOVE_DOWN(Me)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs)

    End Sub

    Private Sub usrcntlVollmacht_AT_Load(sender As Object, e As EventArgs) Handles Me.Load
        initControlsHandler()
        cboSprache.Items.Clear()
        cboSprache.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("Deutsch", "DE"))
        cboSprache.changeItem("Deutsch")

        cbxFA.Items.Clear()
        cbxFA.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("AT-FA", "Finanzamt Österreich,Postfach 260,1000 Wien"))
        cbxFA.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE-FA Dortmund", "Finanzamt Dormund-Unna,Trakehnerweg 4,D-44143 Dortmund"))
        cbxFA.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE-FA Berlin", "Finanzamt Berlin International,Thiemannstr. 1,D-12059 Berlin"))
        cbxFA.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("DE-FA Hannover", "Finanzamt Hannover-Nord,Vahrenwalder Str. 206,D-30165 Hannover"))


        If KdNR > 0 Then initKdNR(KdNR)
    End Sub

    Private Sub pic_Click(sender As Object, e As EventArgs) Handles pic.Click
        FormularManagerNEU.genPDF(Me, True)
    End Sub

    Private Sub cbxFA_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbxFA.SelectedIndexChanged


        Dim faAd1 As String = ""
        Dim faAd2 As String = ""
        Dim faAd3 As String = ""


        txtFAAdresse1.Text = faAd1
        txtFAAdresse2.Text = faAd2
        txtFAAdresse3.Text = faAd3

        If Not cbxFA._value.ToString.Contains(",") Then Exit Sub

        Try


            Dim searchedString = cbxFA._value.ToString
            Dim firstIndex = searchedString.IndexOf(",", 0)

            faAd1 = searchedString.Substring(0, firstIndex)
            txtFAAdresse1.Text = faAd1
            searchedString = searchedString.Replace(faAd1 & ",", "")

            If Not searchedString.Contains(",") Then
                faAd2 = searchedString
                txtFAAdresse2.Text = faAd2
                Exit Sub
            End If

            firstIndex = searchedString.IndexOf(",", 0)
            faAd2 = searchedString.Substring(0, firstIndex)
            txtFAAdresse2.Text = faAd2

            searchedString = searchedString.Replace(faAd2 & ",", "")

            If Not searchedString.Contains(",") Then
                faAd3 = searchedString
                txtFAAdresse3.Text = faAd3
                Exit Sub
            End If

            firstIndex = searchedString.IndexOf(",", 0)
            faAd3 = searchedString.Substring(0, firstIndex)


        Catch ex As Exception
            MsgBox("Fehler in der Funktion '" & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'" & vbNewLine & vbNewLine & ex.Message & vbNewLine & vbNewLine & ex.StackTrace)
        End Try

    End Sub
End Class
