Skip to main content

Get form elements

Get values ​​

When getting form controls, if you want to get form elements flat, use the Elements property of the IForm object.

public void EnumerateElements(ICommandContext c, ICommandParams p) 
{
IForm form = c.App.Workspace.CurrentEditor as IForm;
if (form == null) return;

//Get the controls of the displayed form
IFormElementCollection elements = form.Elements;
c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");

foreach ( IFormElement element in elements )
{
//Output the type of the control
c.App.Output.WriteLine("sample", $" Element: {element.ControlType}");
}
}
info

The above sample code uses the following experimental API.

  • IEditorDef.Name property

These APIs are not guaranteed to be of high quality and are not listed in the API specifications. Use at your own risk.

If you want to get form elements in a tree structure, use IFormElement.Controls. You need to get the root control of the form with the RootControl property of the IForm object, and then recursively get the controls from there.

public void EnumerateFormElements(ICommandContext c, ICommandParams p) 
{
IForm form = c.App.Workspace.CurrentEditor as IForm;
if (form == null) return;

//Get the root control of the displayed form
IFormElement rootControl = form.RootControl;
c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");

//Get form elements recursively
var formElements = GetRecursivelyFormElements(c, rootControl, 0);
foreach(IFormElement element in formElements)
{
//...
}
}

//Get form elements recursively
private IEnumerable<IFormElement> GetRecursivelyFormElements(ICommandContext c, IFormElement element, int hierarchy)
{
var empty = new string(' ', hierarchy);
var elementDefinitionName = element.ElementDefinition == null ? "No element definition" : element.ElementDefinition.Name;
c.App.Output.WriteLine("sample", $"{empty}ColumnType: {element.ControlType}, Element: {elementDefinitionName}");

if (!element.Controls.Any())
{
return Enumerable.Empty<IFormElement>();
}

return new []{ element }.Union(element.Controls.SelectMany(control => GetRecursivelyFormElements(c, control, hierarchy + 1)));
}
info

The above sample code uses the following API, which is implemented experimentally.

  • IEditorDef.Name property
  • IElementDef.Name property

These APIs are not guaranteed for quality and are not listed in the API specifications. Use them at your own risk.

To get a value from a control, use the GetValue method of the IEditorElement object.

public void EnumerateElements(ICommandContext c, ICommandParams p) 
{
//Omitted
foreach ( IFormElement element in elements )
{
c.App.Output.WriteLine("sample", $" Element: {element.ControlType}");

var value = element.GetValue();
if(value is IModel model)
{
//If the control represents a model, the model can be obtained.
c.App.Output.WriteLine("sample", $" Class: {model.ClassName}, Name: {model.Name}");
continue;
}

//If the control represents a field value, the field value can be obtained.
c.App.Output.WriteLine("sample", $" Value: {value}");
}
}

Obtain grid columns

If the form element is a grid (the control type is IGrid type) In this case, you can get column information by using the Columns property of the IGrid object.

public void EnumerateGridColumns(ICommandContext c, ICommandParams p) 
{
IForm form = c.App.Workspace.CurrentEditor as IForm;
if (form == null) return;

//Get the controls of the displayed form
IFormElementCollection elements = form.Elements;
c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");

foreach ( IFormElement element in elements )
{
c.App.Output.WriteLine("sample", $" Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
if(element is IGrid grid)
{
//If the control is a grid, output the column information
foreach(IGridColumn column in grid.Columns)
{
c.App.Output.WriteLine("sample", $" ColumnType: {column.DataTypeName}, Path: {column.Path}");
}
}
}
}
info

The above sample code uses the following APIs, which have been experimentally implemented.

  • IEditorDef.Name property
  • IElementDef.Name property

These APIs are not guaranteed to be of high quality and are not listed in the API specifications. Use them at your own risk.

Getting grid rows

You can also get row objects from the grid by using the Rows property of the IGrid object.

public void EnumerateGridRows(ICommandContext c, ICommandParams p) 
{
IForm form = c.App.Workspace.CurrentEditor as IForm;
if (form == null) return;

//Get the controls of the displayed form
IFormElementCollection elements = form.Elements;

//Process each control
c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
foreach ( IFormElement element in elements )
{
c.App.Output.WriteLine("sample", $" Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
if(element is IGrid grid)
{
foreach(IGridRow row in grid.Rows)
{
//If the control is a grid, output the model name corresponding to the row
c.App.Output.WriteLine("sample", $" RowName: {row.Model.Name}");
}
}
}
}
info

The above sample code uses the following APIs, which have been experimentally implemented.

  • IEditorDef.Name property
  • IElementDef.Name property

These APIs are not guaranteed to be of high quality and are not listed in the API specifications. Use them at your own risk.

Getting the value of a grid cell

To get the value of a cell from a grid row, use the GetValue method of the IGridCell object.

public void EnumerateGridRows(ICommandContext c, ICommandParams p) 
{
IForm form = c.App.Workspace.CurrentEditor as IForm;
if (form == null) return;

//Get the controls of the displayed form
IFormElementCollection elements = form.Elements;

//Process each control
c.App.Output.WriteLine("sample", $"Form : {form.EditorDefinition.Name}");
foreach ( IFormElement element in elements )
{
c.App.Output.WriteLine("sample", $" Type: {element.ControlType}, Element: {element.ElementDefinition.Name}");
if(element is IGrid grid)
{
foreach(IGridRow row in grid.Rows)
{
//If the control is a grid, output the model name corresponding to the row
c.App.Output.WriteLine("sample", $" RowName: {row.Model.Name}");
IGridCellCollection cells = row.Cells;
foreach(IGridCell cell in cells)
{
//If the cell exists in the grid, output the cell's value
var value = cell.GetValue();
if(value is IModel model)
{
c.App.Output.WriteLine("sample", $" Cell: {model.Name}");
continue;
}
c.App.Output.WriteLine("sample", $" Cell: {value}");
}
}
}
}
info

The above sample code uses the following API, which is implemented experimentally.

  • IEditorDef.Name property
  • IElementDef.Name property

These APIs are not guaranteed for quality and are not listed in the API specifications. Use them at your own risk.