Public Class usrCntlATLAS_AES_Positionen

    Dim NCTS As DAKOSY_Worker.cDakosyAES = Nothing

    Dim POS As New List(Of usrCntlATLAS_AES_Position)
    Dim sectionChangeBLOCK = False
    Dim gesRohmasse = 0

    Event RohmasseChanged(gesRohmasse As Double)
    Event PosAnzChanged(anzahlPos As Double)


    Sub New(NCTS)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
        Me.NCTS = NCTS
    End Sub
    Private Sub usrCntlATLAS_ncts_Load(sender As Object, e As EventArgs) Handles Me.Load

        addItem()
        'setValues(NCTS.ncts_WARENPOS)
    End Sub

    Public Sub setValues(ncts_POS As DAKOSY_Worker.cDakosyAES)
        If ncts_POS Is Nothing Then Exit Sub
        POS.Clear()

        For Each p In ncts_POS.aes_WARENPOS
            addItem(p) 'MyPanel2.Controls.Add()
        Next
        MyPanel2.Controls.Clear()
        MyListBox1_SelectedIndexChanged(dgvPositionen, New EventArgs)
    End Sub



    Sub initListBox(Optional selectedIndex = -1)
        If dgvPositionen.ColumnCount <= 0 Then Exit Sub
        If selectedIndex >= 0 Then sectionChangeBLOCK = True
        Dim cnt As Integer = 1
        dgvPositionen.Rows.Clear()
        For Each p In POS
            p.lblPosNr.Text = cnt
            dgvPositionen.Rows.Add(POS.IndexOf(p), cnt, p.txtWarenbezeichnung.Text)
            cnt += 1
        Next
        If selectedIndex >= 0 Then
            '   MyListBox1.SetSelected(selectedIndex, True)
            '  sectionChangeBLOCK = False
        End If
    End Sub

    Private Sub dgvPositionen_KeyDown(sender As Object, e As KeyEventArgs) Handles dgvPositionen.KeyDown
        If e.KeyCode = Keys.Delete Then
            removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
        End If
    End Sub


    Private Sub MyListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles dgvPositionen.SelectionChanged
        Try

            If sectionChangeBLOCK Then Exit Sub
            MyPanel2.Controls.Clear()
            If dgvPositionen.SelectedRows.Count > 0 Then
                MyPanel2.Controls.Add(POS(dgvPositionen.SelectedRows(0).Cells("index").Value))
                POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
                '   Try : DirectCast(MyPanel2.Controls(0), usrCntlATLAS_aes_Position).txtWarenbezeichnung.Focus() : Catch : End Try
                ' initListBox(MyListBox1.SelectedIndex())
            End If

        Catch ex As Exception

        End Try
    End Sub

    Private Sub usrCntlATLAS_aes_Position_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
        If e.Control = True Then
            If e.KeyCode = Keys.Oemplus Then addItem()
            If e.KeyCode = Keys.OemMinus Then
                If dgvPositionen.SelectedRows.Count > 0 Then
                    removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
                End If
            End If

        End If
    End Sub



    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
        Try
            If keyData = (Keys.Control Or Keys.Add) Then
                'RaiseEvent a() : Return True
                addItem()
                Return True
            ElseIf keyData = (Keys.Control Or Keys.Subtract) Then
                'RaiseEvent REMOVE(ID) : Return True
                removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
                Return True
            ElseIf keyData = Keys.PageUp Then
                'RaiseEvent PREV_POS() : Return True
                PREV_POS()
                Return True
            ElseIf keyData = Keys.PageDown Then
                'RaiseEvent NEXT_POS() : Return True
                NEXT_POS()
                Return True
            End If
        Catch ex As Exception
        End Try
        Return False
    End Function


    Private Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
        addItem()
    End Sub


    Function addItem(Optional POSTMP As DAKOSY_Worker.cDakosy_AES_Warenposition = Nothing) As usrCntlATLAS_AES_Position
        Dim u = New usrCntlATLAS_AES_Position(POS.Count, POSTMP)

        AddHandler u.RohmasseChanged, Sub(r)
                                          gesRohmasse = 0
                                          For Each p In POS
                                              gesRohmasse += If(IsNumeric(p.txtRohmasse.Text), p.txtRohmasse.Text, 0)
                                          Next
                                          RaiseEvent RohmasseChanged(gesRohmasse)
                                      End Sub
        u.Dock = DockStyle.Fill
        '  AddHandler u.ADD, AddressOf addItem
        ' AddHandler u.REMOVE, AddressOf removeItem
        ' AddHandler u.NEXT_POS, AddressOf NEXT_POS
        ' AddHandler u.PREV_POS, AddressOf PREV_POS
        POS.Add(u)
        initListBox()
        dgvPositionen.ClearSelection()
        If dgvPositionen.RowCount > 0 Then dgvPositionen.Rows(dgvPositionen.RowCount - 1).Selected = True
        RaiseEvent PosAnzChanged(POS.Count)
        Return u
    End Function

    Sub removeItem(index)
        If index < 0 Then Exit Sub
        'If index < 0 Then Exit Sub
        If dgvPositionen.Rows.Count > index Then
            If vbYes = MsgBox("Möchten Sie die Position wirklich löschen?", vbYesNoCancel) Then
                dgvPositionen.ClearSelection()
                POS.RemoveAt(index)
                initListBox()
                If dgvPositionen.Rows.Count > index Then
                    dgvPositionen.Rows(index).Selected = True 'SetSelected(index, True)
                ElseIf dgvPositionen.Rows.Count > 0 Then
                    dgvPositionen.Rows(0).Selected = True
                End If
            End If
        End If
        RaiseEvent PosAnzChanged(POS.Count)
    End Sub

    Sub NEXT_POS()
        If dgvPositionen.SelectedRows.Count > 0 Then
            If dgvPositionen.RowCount > dgvPositionen.SelectedRows(0).Index + 1 Then
                Dim i = dgvPositionen.SelectedRows(0).Index
                dgvPositionen.ClearSelection()
                dgvPositionen.Rows(i + 1).Selected = True
                '  POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
            End If
        End If
    End Sub
    Sub PREV_POS()
        If dgvPositionen.SelectedRows.Count > 0 Then
            If dgvPositionen.SelectedRows(0).Index > 0 Then
                Dim i = dgvPositionen.SelectedRows(0).Index
                dgvPositionen.ClearSelection()
                dgvPositionen.Rows(i - 1).Selected = True
                ' POS(dgvPositionen.SelectedRows(0).Cells("index").Value).txtWarenbezeichnung.Focus()
            End If
        End If
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If dgvPositionen.SelectedRows.Count > 0 Then
            removeItem(dgvPositionen.SelectedRows(0).Cells("index").Value)
        End If
    End Sub

    Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick
        refreshDGV()
    End Sub


    Public Sub refreshDGV()
        With dgvPositionen
            If dgvPositionen.ColumnCount = 0 Then Exit Sub
            For Each r As DataGridViewRow In .Rows
                Dim index = r.Cells("index").Value
                'r.Cells("ID").Value = POS(index)..Text
                If POS.Count > index Then
                    r.Cells("Ware").Value = POS(index).txtWarenbezeichnung.Text
                End If
            Next
        End With
    End Sub


    Public Sub getValues(ByRef NCTS As DAKOSY_Worker.cDakosyAES)
        '   Dim POSITIONEN As List(Of DAKOSY_Worker.cDakosy_NCTSOut_Warenposition)
        NCTS.aes_WARENPOS.Clear()

        Dim cnt As Integer = 1
        For Each P In POS
            Dim POSITION As New DAKOSY_Worker.cDakosy_AES_Warenposition

            NCTS.aes_WARENPOS.Add(POSITION)
            cnt += 1
        Next
        '    NCTS.ncts_WARENPOS
    End Sub


    Private Sub MyPanel2_ControlAdded(sender As Object, e As ControlEventArgs) Handles MyPanel2.ControlAdded
        Try : DirectCast(e.Control, usrCntlATLAS_aes_Position).txtWarenbezeichnung.Focus() : Catch : End Try
    End Sub
End Class
