The current recipe will cover the topic of creating a custom textbox control bound to a database field. Such control can be linked with a table field via the standard NAV property, SourceExpr
. Any changes made by the user in a control linked to a table field are automatically reflected in the database.
NavDatabaseFieldControl
as the name of the project.Class1
in the solution explorer. Assign a new name, DatabaseFieldControl
, and accept renaming all code references.Microsoft.Dynamics.Framework.UI.Extensibility.dll
and System.Windows.Forms
.using System; using Microsoft.Dynamics.Framework.UI.Extensibility; using Microsoft.Dynamics.Framework.UI.Extensibility.WinForms; using System.Windows.Forms;
ControlAddInExport
attribute:namespace NavDatabaseFieldControl { [ControlAddInExport("NavDatabaseFieldControl")] public class DatabaseFieldControl : WinFormsControlAddInBase, IObjectControlAddInDefinition { } }
private TextBox textBox; private bool valueChanged;
IObjectControlAddInDefinition
:public event ControlAddInEventHandler ControlAddIn; public bool HasValueChanged { get { return valueChanged; } } public object Value { get { return textBox.Text; } set { textBox.Text = (string)value; valueChanged = false; } }
DatabaseFieldControl
class is CreateControl
:protected override Control CreateControl() { textBox = new TextBox(); textBox.TextChanged += TextBox_TextChanged; ControlAddIn(0, string.Empty); return textBox; }
Editable
property that will control if the user is allowed to edit the field:[ApplicationVisible] public bool Editable { get { return !textBox.ReadOnly; } set { textBox.ReadOnly = !value; } }
TextBox
control has changed:private void TextBox_TextChanged(object sender, EventArgs e) { valueChanged = true; }
NavDatabaseFieldControl.dll
assembly file to the client add-ins location.SourceTable
property to Customer
.
Type |
SubType |
SourceExpr |
Name |
Container |
ContentArea |
Customer | |
Field |
"No." |
CustomerNo | |
Field |
Name |
CustomerName |
ControlAddIn
property open the list of client add-ins. Select NavDatabaseFieldControl from the list.CustomerNo - OnControlAddIn
event handler type the following code:CurrPage.CustomerNo.Editable := FALSE;
The first four steps of this recipe add references to external libraries. In step 5, the DatabaseFieldControl
class that will implement the database-bound control is declared. This class is inherited from WinFormsControlAddInBase
. Besides, the control class must implement an IObjectControlAddInDefinition
interface in order to enable the connection between the UI component and data field.
The ControlAddIn
event and two public properties, Value
and HasValueChanged
, are members of IObjectControlAddInDefinition
that must be implemented in the class. All these interface members are implemented in step 7.
The ControlAddIn
event is raised in the CreateControl
method to notify the calling function (in our case, this will be a C/AL function) that the control is created and ready.
Step 9 defines an additional Editable
property that is not required by the interface definition, but will be very useful to protect a control from editing. To make the property accessible from the NAV page designer, we mark with the [ApplicationVisible]
attribute.
Step 15 through Step 19 demonstrate how to use the developed component. Two fields from the Customer table are displayed in a page in a custom textbox controls instead of standard NAV fields. To assign a table field to a control add-in, we use the standard SourceExpr
property. No additional C/AL code is required to process the data. When a record is retrieved from the table, the NAV platform updates the value of the control through the IObjectControlAddInDefinition
interface. The same way, the new value is automatically saved by the platform when the user updates the control content.
18.226.165.131