Imports IntraSell_DLL

Public Class LieferantControl

    Dim loading As Boolean

    Public Event IDNRChanged(IDNR As Integer) 'wenn der User einen neuen Objekt gewählt hat 

    Public Const SQL = "SELECT IDNR, Firma " &
                       "FROM `lieferantenAdressen` ORDER BY Firma"
    Private _IDNR As Integer

    Public Property IDNR() As Integer
        Get
            Return _IDNR
        End Get
        Set(value As Integer)
            _IDNR = value
            ObjektComboBox.SelectedValue = _IDNR
        End Set
    End Property

    Public Property ShowObjektSelectorButton() As Boolean
        Get
            Return ObjektSelectorButton.Visible
        End Get
        Set(value As Boolean)
            ObjektSelectorButton.Visible = value
        End Set
    End Property

    Public Property ShowAddNew() As Boolean
        Get
            Return AddNewButton.Visible
        End Get
        Set(value As Boolean)
            AddNewButton.Visible = value
        End Set
    End Property

    Private Sub LieferantenControl_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
        FillComboBox(ObjektComboBox, SQL, "Firma", "IDNR")
        ObjektComboBox.Text = ""
    End Sub

    Private Sub ObjektSelectorButton_Click(sender As Object, e As EventArgs) Handles ObjektSelectorButton.Click
        Try
            Lieferant.Show()
            Lieferant.FilterBy("IDNR=" & IDNR)
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub ObjektComboBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ObjektComboBox.SelectedIndexChanged
        Try
            If IsNumeric(ObjektComboBox.SelectedValue) Then
                IDNR = ObjektComboBox.SelectedValue
                RaiseEvent IDNRChanged(IDNR)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    ''' <summary>
    ''' Clean for Filters etc.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ObjektComboBox_TextUpdate(sender As Object, e As System.EventArgs) Handles ObjektComboBox.TextUpdate
        Try
            If ObjektComboBox.Text = "" Then
                IDNR = Nothing
                RaiseEvent IDNRChanged(Nothing)
            End If
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub


    Private Sub AddNewButton_Click(sender As Object, e As EventArgs) Handles AddNewButton.Click
        Try
            Dim a As New Lieferant With {
                .BeginNewFlag = True
            }
            a.ShowDialog()
            loading = True
            FillComboBox(ObjektComboBox, SQL, "Firma", "IDNR")
            loading = False
        Catch ex As Exception
            HandleAppError(ex)
        End Try
    End Sub

    Private Sub ObjektComboBox_Enter(sender As Object, e As EventArgs) Handles ObjektComboBox.Enter
        Try
            FillComboBox(ObjektComboBox, SQL, "Firma", "IDNR")
        Catch ex As Exception
            'Ignore
        End Try
    End Sub
End Class
