Skip to main content
.NET 6.0+

How to: Initialize an Object Created Using the New Action

  • 3 minutes to read

This topic describes how to access an object that is created using the New Action. Assume you are using the Task business class from the Business Class Library. When creating a new Task using the New Action, the Task.StartDate property will be set to the current date.

To access an object created using the New Action, handle the NewObjectViewController.ObjectCreated event of the NewObjectViewController that contains the New Action. To do this, implement a new View Controller. Override the Controller’s OnActivated method and subscribe to the ObjectCreated event in the following manner:

using DevExpress.Persistent.BaseImpl;
using DevExpress.ExpressApp.SystemModule;
//...
public class MyController : ViewController {
    private NewObjectViewController controller;
    protected override void  OnActivated() {
        base.OnActivated();
        controller = Frame.GetController<NewObjectViewController>();
        if (controller != null) {
            controller.ObjectCreated += controller_ObjectCreated;
        }
    }
    void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) {
        if (e.CreatedObject is Task) {
            ((Task)e.CreatedObject).StartDate = DateTime.Now;
        }
    }
    protected override void OnDeactivated() {
        if (controller != null) {
            controller.ObjectCreated -= controller_ObjectCreated;
        }
        base.OnDeactivated();
    }
}

In certain scenarios, it can be required to initialize a new object created through the lookup editor’s New button, using a value from the parent Detail View. To access the parent object from the ObjectCreated event handler, cast the Controller.Frame value to the NestedFrame type, access the NestedFrame.ViewItem property and then get the master object using the ViewItem.CurrentObject property.

void controller_ObjectCreated(object sender, ObjectCreatedEventArgs e) {
    NestedFrame nestedFrame = Frame as NestedFrame;
    if (nestedFrame != null) {
        Item createdItem = e.CreatedObject as Item;
        if (createdItem != null) {
            Parent parent = ((NestedFrame)Frame).ViewItem.CurrentObject as Parent;
            if (parent != null) {
                createdItem.Title = parent.DefaultItemTitle;
            }
        }
    }
}
See Also