Linking add-ins with the database

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.

How to do it...

  1. Run Visual Studio and create a Class Library project. Choose to create a new solution and enter NavDatabaseFieldControl as the name of the project.
  2. Rename the class Class1 in the solution explorer. Assign a new name, DatabaseFieldControl, and accept renaming all code references.
  3. In the solution explorer window, add references to assemblies Microsoft.Dynamics.Framework.UI.Extensibility.dll and System.Windows.Forms.
  4. Remove default using statements and replace them with the following:
            using System; 
            using Microsoft.Dynamics.Framework.UI.Extensibility; 
            using Microsoft.Dynamics.Framework.UI.Extensibility.WinForms; 
            using System.Windows.Forms; 
    
  5. Add references to the base class and interface to the class declaration, and insert the ControlAddInExport attribute:
            namespace NavDatabaseFieldControl 
            { 
              [ControlAddInExport("NavDatabaseFieldControl")] 
              public class DatabaseFieldControl : WinFormsControlAddInBase,  
                IObjectControlAddInDefinition 
              { 
              } 
            } 
    
  6. Declare two private properties in the class:
            private TextBox textBox; 
            private bool valueChanged; 
    
  7. After the private declarations, insert implementation of interface members inherited from 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; 
              } 
            } 
    
  8. The last method declared in the interface that must be implemented in the DatabaseFieldControl class is CreateControl:
            protected override Control CreateControl() 
            { 
              textBox = new TextBox(); 
              textBox.TextChanged += TextBox_TextChanged; 
     
              ControlAddIn(0, string.Empty); 
     
              return textBox; 
            } 
    
  9. Insert the following code to declare a 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; } 
            } 
    
  10. Type the code of the event handler called when the text in the TextBox control has changed:
            private void TextBox_TextChanged(object sender, EventArgs e) 
            { 
              valueChanged = true; 
            } 
    
  11. Sign the assembly (see the Signing the control add-in assembly recipe for details on how to do it).
  12. Build the project and copy the NavDatabaseFieldControl.dll assembly file to the client add-ins location.
  13. Run NAV client and register the control. The registration procedure is described in the Registering and embedding a control add-in recipe.
  14. In NAV page designer, create a blank page. Open properties of the new page and set the value of the SourceTable property to Customer.
  15. In the page designer, configure the following controls:

    Type

    SubType

    SourceExpr

    Name

    Container

    ContentArea

    Customer

    Field

    "No."

    CustomerNo

    Field

    Name

    CustomerName

  16. Access properties of the CustomerNo field, and in the ControlAddIn property open the list of client add-ins. Select NavDatabaseFieldControl from the list.
  17. The CustomerName field should refer to the same control add-in, so repeat step 16 for this field.
  18. Open C/AL code editor. In the CustomerNo - OnControlAddIn event handler type the following code:
            CurrPage.CustomerNo.Editable := FALSE; 
    
  19. Save the page and run it to view and edit customer records in custom controls.

How it works...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.131