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.Editors
Imports System.Windows.Forms
Imports DevExpress.XtraLayout


Public Class DateTimeHandle
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()

        'This call is required by the Component Designer.
        InitializeComponent()
        RegisterActions(components)
        TargetViewType = ViewType.DetailView

    End Sub
    Protected Overloads Overrides Sub OnActivated()
        MyBase.OnActivated()
        For Each editor As PropertyEditor In (CType(View, DetailView)).GetItems(Of PropertyEditor)()

            AddHandler editor.ControlCreated, AddressOf editor_ControlCreated
        Next editor

    End Sub
    Private Sub editor_ControlCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' You can access the corresponding property editor control by the following code:
        Dim editorControl As Control = TryCast((CType(sender, PropertyEditor)).Control, Control)
        AddHandler editorControl.HandleCreated, AddressOf editorControl_HandleCreated

        Dim _DateTimeEditor As DevExpress.XtraEditors.DateEdit = TryCast(editorControl, DevExpress.XtraEditors.DateEdit)
        If _DateTimeEditor IsNot Nothing Then
            _DateTimeEditor.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTimeAdvancingCaret
            AddHandler _DateTimeEditor.Click, AddressOf DateTimeEditor_Click
            AddHandler _DateTimeEditor.KeyDown, AddressOf DateTimeEditor_KeyDown

        End If
        Dim _SpinEditor As DevExpress.XtraEditors.SpinEdit = TryCast(editorControl, DevExpress.XtraEditors.SpinEdit)
        If _SpinEditor IsNot Nothing Then
            AddHandler _SpinEditor.Click, AddressOf SpinEditor_Click
            AddHandler _SpinEditor.KeyDown, AddressOf SpinEditor_KeyDown
        End If

        Dim _TextEditor As DevExpress.XtraEditors.TextEdit = TryCast(editorControl, DevExpress.XtraEditors.TextEdit)
        If _TextEditor IsNot Nothing Then
            AddHandler _TextEditor.KeyDown, AddressOf TextEditor_KeyDown


        End If
    End Sub
    Private Sub editorControl_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
        ' Sometimes it is necessary to access editor's control after it is visible (its handle is created).
        RemoveHandler (CType(sender, Control)).HandleCreated, AddressOf editorControl_HandleCreated
    End Sub

    Private Sub DateTimeEditor_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        sender.SelectAll()
    End Sub
    Private Sub SpinEditor_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        sender.Selectall()
    End Sub

    Private Sub DateTimeEditor_KeyDown(sender As Object, e As KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Enter, Keys.Down
                DirectCast(View.Control, LayoutControl).FocusHelper.GetNextControl(sender).Focus()
                e.Handled = True
            Case Else

        End Select
    End Sub

    Private Sub SpinEditor_KeyDown(sender As Object, e As KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Enter, Keys.Down
                DirectCast(View.Control, LayoutControl).FocusHelper.GetNextControl(sender).Focus()
                e.Handled = True
            Case Else

        End Select
    End Sub

    Private Sub TextEditor_KeyDown(sender As Object, e As KeyEventArgs)
        If TryCast(sender, DevExpress.ExpressApp.Win.Editors.LargeStringEdit) IsNot Nothing Then
        Else
            Select Case e.KeyCode
                Case Keys.Enter, Keys.Down
                    DirectCast(View.Control, LayoutControl).FocusHelper.GetNextControl(sender).Focus()
                    e.Handled = True
                Case Keys.Up
                    DirectCast(View.Control, LayoutControl).FocusHelper.ProcessDialogKey(Keys.Shift + Keys.Tab)
                    e.Handled = True
                Case Else

            End Select
        End If


    End Sub

End Class
