Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports DevExpress.ExpressApp.Win.Editors

Imports DevExpress.XtraGrid
Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.ExpressApp.Editors
Public Class ProductSelect_VC
    Inherits DevExpress.ExpressApp.ViewController
    Private _GridListEditor_Select As GridListEditor
    Private Delegate Sub EditCellDelegate()
    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)

    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()
        If View.Id = "ProductSelect_LookupListView" Then
            _GridListEditor_Select = TryCast(TryCast(View, ListView).Editor, GridListEditor)

            For Each column As DevExpress.XtraGrid.Columns.GridColumn In _GridListEditor_Select.GridView.Columns
                column.OptionsFilter.ImmediateUpdateAutoFilter = False
                If column.FieldName = "Products.ShortName" Then
                    column.OptionsFilter.AutoFilterCondition = Columns.AutoFilterCondition.Contains
                End If
            Next
            If _GridListEditor_Select IsNot Nothing Then
                AddHandler _GridListEditor_Select.ColumnCreated, AddressOf ProductSelect_LookupListView_ColumnCreated
                AddHandler _GridListEditor_Select.GridView.ColumnFilterChanged, AddressOf ProductSelect_LookupListView_ColumnFilterChanged
                AddHandler _GridListEditor_Select.GridView.CellValueChanged, AddressOf ProductSelect_LookupListView_ListView_CellValueChanged
            End If
        End If
    End Sub

    Private Sub ProductSelect_LookupListView_ColumnCreated(sender As Object, e As ColumnCreatedEventArgs)
        e.Column.OptionsFilter.ImmediateUpdateAutoFilter = False
    End Sub

    Private Sub ProductSelect_LookupListView_ColumnFilterChanged(sender As Object, e As EventArgs)
        Dim timer As System.Timers.Timer = New System.Timers.Timer(200)
        AddHandler timer.Elapsed, AddressOf Timer_Elapsed
        timer.Enabled = True
        timer.AutoReset = False
    End Sub

    Private Sub ProductSelect_LookupListView_ListView_CellValueChanged(sender As Object, e As CellValueChangedEventArgs)
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        If e.Column.FieldName = "QTT_Will" Then
            'Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace

            'Dim _ListView As ListView = TryCast(View, ListView)
            'Dim _PropertyCollectionSource As PropertyCollectionSource = TryCast(_ListView.CollectionSource, PropertyCollectionSource)
            'Dim _ProductFinder As ProductFinder = TryCast(_PropertyCollectionSource.MasterObject, ProductFinder)


            'Dim _ProductNumber As String = _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "Products.ProductNumber")
            'Dim _Products As Products = _onew.FindObject(Of Products)(CriteriaOperator.Parse("ProductNumber=?", _ProductNumber))

            'Dim _ProductSelected As ProductSelected = _onew.FindObject(Of ProductSelected)(CriteriaOperator.Parse("MasterKey=? AND Products=?", _ProductFinder.MasterKey, _Products))
            'If _ProductSelected Is Nothing Then
            '    _ProductSelected = _onew.CreateObject(Of ProductSelected)()
            '    _ProductSelected.Products = _Products
            '    _ProductSelected.MasterKey = _ProductFinder.MasterKey
            '    _ProductSelected.QTT = _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "QTT_Will")
            'Else
            '    _ProductSelected.QTT += _GridListEditor_Select.GridView.GetRowCellValue(e.RowHandle, "QTT_Will")
            'End If

            '_onew.CommitChanges()

        End If

        If _GridListEditor_Select.GridView.FocusedColumn Is _GridListEditor_Select.GridView.Columns("QTT_Will") Then
            _GridListEditor_Select.GridView.FocusedColumn = _GridListEditor_Select.GridView.GetVisibleColumn(_GridListEditor_Select.FindColumn("Products.ProductNumber").VisibleIndex)
            _GridListEditor_Select.GridView.FocusedRowHandle = GridControl.AutoFilterRowHandle
            _GridListEditor_Select.GridView.ShowEditor()
            If _GridListEditor_Select.GridView.ActiveEditor IsNot Nothing Then
                _GridListEditor_Select.GridView.ActiveEditor.Focus()
            End If
        End If
        If e.Column.FieldName = "QTT_Will" Then
            'Dim _DetailView As DetailView = TryCast(TryCast(Frame, NestedFrame).ViewItem.View, DetailView)
            'If _DetailView IsNot Nothing Then
            '    Dim _ListPropertyEditor As ListPropertyEditor = _DetailView.FindItem("ProductSelected")
            '    If _ListPropertyEditor IsNot Nothing Then
            '        TryCast(_ListPropertyEditor.ListView, ListView).CollectionSource.Reload()
            '    End If
            'End If
        End If
    End Sub
    Private Sub Timer_Elapsed()
        _GridListEditor_Select.GridView.GridControl.Invoke(New EditCellDelegate(AddressOf Edit_QTT_Will_Cell))
    End Sub
    Private Sub Edit_QTT_Will_Cell()
        'e.KeyCode = Windows.Forms.Keys.Enter And 
        If _GridListEditor_Select.GridView.FocusedRowHandle = GridControl.AutoFilterRowHandle And _GridListEditor_Select.GridView.RowCount >= 1 Then
            _GridListEditor_Select.GridView.FocusedColumn = _GridListEditor_Select.GridView.Columns("QTT_Will")
            _GridListEditor_Select.GridView.HideEditor()
            _GridListEditor_Select.GridView.FocusedRowHandle = _GridListEditor_Select.GridView.GetVisibleRowHandle(0)
            _GridListEditor_Select.GridView.ShowEditor()
            If _GridListEditor_Select.GridView.ActiveEditor IsNot Nothing Then
                _GridListEditor_Select.GridView.ActiveEditor.Focus()
            End If
        End If
    End Sub
End Class
