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 ServiceFinderVC
    Inherits DevExpress.ExpressApp.ViewController(Of ListView)
    Private _GridListEditor_Selected As GridListEditor
    Private _GridListEditor_Select As GridListEditor
    Private gridView As ColumnView
    Private currentView As ListView
    Private falseCriteria As CriteriaOperator = CriteriaOperator.Parse("1=0")
    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 OnActivated()
        MyBase.OnActivated()
        If View.Id = "ServiceFinder_ServiceSelect_ListView" Then
            currentView = CType(View, ListView)
            AddHandler currentView.ControlsCreated, AddressOf AutoFilterRowController_ControlsCreated
        End If
    End Sub
    Protected Overrides Sub OnViewControlsCreated()
        MyBase.OnViewControlsCreated()

        If View.Id = "ServiceFinder_ServiceSelected_ListView" Then
            _GridListEditor_Selected = TryCast(View.Editor, GridListEditor)

            If _GridListEditor_Selected IsNot Nothing Then
                AddHandler _GridListEditor_Selected.GridView.CellValueChanged, AddressOf ServiceFinder_ServiceSelected_ListView_CellValueChanged
            End If


        End If
        If View.Id = "ServiceFinder_ServiceSelect_ListView" 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 = "Services.ShortName" Then
                    column.OptionsFilter.AutoFilterCondition = Columns.AutoFilterCondition.Contains
                End If
            Next
            If _GridListEditor_Select IsNot Nothing Then
                AddHandler _GridListEditor_Select.ColumnCreated, AddressOf Services_ListView_ColumnCreated
                AddHandler _GridListEditor_Select.GridView.ColumnFilterChanged, AddressOf GridView_ColumnFilterChanged
                AddHandler _GridListEditor_Select.GridView.CellValueChanged, AddressOf ServiceFinder_ServiceSelect_ListView_CellValueChanged
                AddHandler _GridListEditor_Select.GridView.RowClick, AddressOf ServiceFinder_ServiceSelect_RowClick
            End If
        End If

    End Sub
    Private Sub AutoFilterRowController_ControlsCreated(ByVal sender As Object, ByVal e As EventArgs)
        Dim grid As GridControl = TryCast(currentView.Control, GridControl)
        gridView = TryCast(grid.FocusedView, ColumnView)
        If gridView.ActiveFilter.IsEmpty Then
            currentView.CollectionSource.Criteria("FalseCriteria") = falseCriteria
        End If
        AddHandler gridView.ActiveFilter.Changed, AddressOf ActiveFilter_Changed
    End Sub
    Private Sub ActiveFilter_Changed(ByVal sender As Object, ByVal e As EventArgs)
        If (CType(sender, ViewFilter)).IsEmpty Then
            currentView.CollectionSource.Criteria("FalseCriteria") = falseCriteria
        Else
            If currentView.CollectionSource.Criteria.ContainsKey("FalseCriteria") Then
                currentView.CollectionSource.Criteria.Remove("FalseCriteria")
            End If
        End If
    End Sub

    Private Sub ServiceFinder_ServiceSelected_ListView_CellValueChanged(sender As Object, e As CellValueChangedEventArgs)

    End Sub
    Private Sub Services_ListView_ColumnCreated(sender As Object, e As ColumnCreatedEventArgs)
        e.Column.OptionsFilter.ImmediateUpdateAutoFilter = False
    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.GetVisibleColumn(_GridListEditor_Select.FindColumn("QTT_Will").VisibleIndex)
            _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
    Private Sub GridView_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 ServiceFinder_ServiceSelect_ListView_CellValueChanged(sender As Object, e As CellValueChangedEventArgs)

    End Sub

    Private Sub ServiceFinder_ServiceSelect_RowClick(sender As Object, e As Views.Grid.RowClickEventArgs)

    End Sub

End Class
