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.SystemModule
Imports DevExpress.ExpressApp.Editors

Public Class ContractsLookupVC
    Inherits ViewController
    Public Sub New()
        TargetObjectType = GetType(Contracts)
        TargetViewType = ViewType.ListView
        TargetViewId = "Contracts_LookupListView"
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        Dim c As NewObjectViewController = Frame.GetController(Of NewObjectViewController)()
        AddHandler c.ObjectCreated, AddressOf c_ObjectCreated
    End Sub

    Private Sub c_ObjectCreated(ByVal sender As Object, ByVal e As ObjectCreatedEventArgs)
        If TypeOf Frame Is NestedFrame Then
            'for ASP.NET
            Dim editor As PropertyEditor = TryCast((CType(Frame, NestedFrame)).ViewItem, PropertyEditor)
            SetupNewObjectFromParent(e, editor)
        End If
        'for WinForms
        Dim controller As LinkToLookupViewController = Frame.GetController(Of LinkToLookupViewController)()
        If controller IsNot Nothing AndAlso controller.Lookup IsNot Nothing Then
            SetupNewObjectFromParent(e, controller.Lookup)
        End If
    End Sub
    Private Sub SetupNewObjectFromParent(ByVal e As ObjectCreatedEventArgs, ByVal editor As PropertyEditor)
        If GetType(Contracts).IsInstanceOfType(e.CreatedObject) Then
            If editor IsNot Nothing AndAlso GetType(RegistryRowsFinancialControlling).IsAssignableFrom(editor.ObjectType) Then
                Dim r As RegistryRowsFinancialControlling = DirectCast(editor.CurrentObject, RegistryRowsFinancialControlling)
                If Not editor.View.ObjectSpace.IsNewObject(r.RegistryHeader.Customers) Then
                    CType(e.CreatedObject, Contracts).Customers = e.ObjectSpace.GetObject(Of Customers)(r.RegistryHeader.Customers)
                    CType(e.CreatedObject, Contracts).CustomersFrom = e.ObjectSpace.GetObject(Of Customers)(r.RegistryHeader.CustomersFrom)
                End If
            End If
        End If
    End Sub
End Class
