![](https://www.dynamics101.com/wp-content/uploads/2015/03/030515_0052_UsingManage3.png)
Overview
Managed host control in Microsoft Dynamics AX 2012 is used to host a .NET or WPF control on a Microsoft Dynamics AX form. It is used to enhance the usability of the application and provides users with a rich experience design.
The ability to add these controls on a form gives access to many features that are not natively supported by standard Microsoft Dynamics AX. In addition, customized controls can be created in .NET and used as a managed host control. The integration between Microsoft Dynamics AX and .NET controls is seamless and easy to implement. Managed host controls do not support Microsoft Silverlight controls, however, Silverlight controls can be used in the Enterprise portal.
Managed host control uses the CLR interop feature of X++ to access the properties, methods and events of a specified .NET or WPF control. CLR interop is used because it supports implicit conversion of many X++ data types into corresponding .NET CLR data types. For complex data types like collections, some conversion may have to be performed.
Pre-requisites
- Microsoft Dynamics AX 2012
- The assembly of the control to be used must be added as a reference in the Microsoft Dynamics AX references node (AOT à References) and the assembly must be installed on the computer where Microsoft Dynamics AX client is installed
Important Concepts
-
Properties
Properties are used to assign or retrieve the values that define the behavior of a control. Since Microsoft Dynamics AX does not supports properties, all the properties of a .NET control are converted into methods. To assign a property any value, set_<Property Name>() is used and to retrieve a property’s value, where get_<Property Name()> is used.
-
Events
Event refers to any activity or occurrence detected by the system in response to a user or a system action. Both X++ and .NET controls support events. To integrate the events of a .NET control in Microsoft Dynamics AX, event handlers are used. An event handler is a registered form method that gets called every time an event is raised by the system.
To register a .NET control event on a managed host control, a reference of the managed H=host control is obtained from an instance of a .NET control. The instance of the .NET control contains methods with the name add_<Event Name>, which is used to register a .NET event handler in Microsoft Dynamics AX. Microsoft Dynamics AX contains a class ManagedEventHandler whose instance is passed to the add_<Event Name> method.
To register a form method as an event handler, pass the instance of ManagedEventHandler class, which takes the form and name of the method as an input, to the event method of .NET control as a parameter. The necessary business logic is added to the form method, which gets called every time the .NET control raises the event. To unregister an event handler, use the remove_<Event Name> method of the .NET control.
Scenario
As part of this tutorial, all customers will be listed in a .NET grid view control and the selected record of the grid will be shown in an infolog.
Steps
- First of all create a new form. Open AOT à Forms
-
Right click on Forms and create a new Form ManagedHostDemo
-
Navigate to the Design node of the form and add a ManagedHost control.
- The Managed control selector dialog will then open. On the dialog, the top grid lists all the assemblies that are referenced in Microsoft Dynamics AX. To reference a new assembly, the Add reference button is used. The below grid list all the controls available in the selected assembly
- Select System.Windows.Forms from the assembly list and DataGridView from the control list and press OK
- A new managed host control will be added. Rename the control to DataGridViewManagedHost
- Set the Height property to Column height and Width property to Column width of the control
- Since managed host control cannot be used directly to access the methods of a .NET control, the reference of the managed host control must be obtained in a global variable of the same type as .NET control so that different properties and methods can be accessed
-
Open the Class declaration node and write the following code
//declare a variable of datagridview type System.Windows.Forms.DataGridView dataGridView;
-
Override the init method of the form and write the following code in it after the super() call
//get the reference of the managed host control dataGridView = DataGridViewManagedHost.control(); //set row header visible property to false dataGridView.set_RowHeadersVisible(false); //set columns to auto fill dataGridView.set_AutoSizeColumnsMode(System.Windows.Forms.DataGridViewAutoSizeColumnsMode::Fill);
-
Override the run method of the form and write the following code
public void run() { System.Windows.Forms.DataGridViewColumnCollection columnCollection; System.Windows.Forms.DataGridViewRowCollection rowCollection; CustTable custTable; System.String[] strValues; super(); //define the columns of the grid columnCollection = dataGridView.get_Columns(); columnCollection.Add("CustomerAcount","Customer account"); columnCollection.Add("CustomerName","Customer name"); //fill the rows of the grid rowCollection = dataGridView.get_Rows(); while select * from custTable { strValues = new System.String[2](); strValues.set_Item(0, custTable.AccountNum); strValues.set_Item(1, custTable.name()); rowCollection.Add(strValues); } }
-
Now open the form by right clicking the form and select Open
- All the customers will be shown in the .NET grid view control
- Now an event will be registered that will show the selected record in an infolog
- Right click the DataGridViewManagedHost control and select Events
- The Events dialog is then opened. It lists all the events that are available for the control
- Select SelectionChanged event and click on Add button
- It will create a corresponding X++ method and will register the method as an event handler in the init method
- The name of the newly created method is shown in the X++ methods column. Press the Close button to close the form
- Navigate to the init method and verify that the method added above is registered as an event handler. Since SelectionChanged event was to be registered, it is accessed via add_SelectionChanged method of the .NET control. An instance of ManagedEventHandler class having form and method name as input is passed as a parameter to the method
-
Now open the DataGridViewManagedHost_SelectionChanged method and write the following code in it
void DataGridViewManagedHost_SelectionChanged(System.Object sender, System.EventArgs e) { System.Windows.Forms.DataGridViewCell cell; System.Windows.Forms.DataGridViewRow row = dataGridView.get_CurrentRow(); cell = dataGridView.get_Item(0,row.get_Index()); info(cell.get_Value()); }
- Open the form and select any row. The selected customer account will be shown in the infolog