From on Dynamics AX 2012, it is possible to add any .NET Windows Forms control to Dynamics AX Forms and use it directly inside Dynamics AX, which adds much more power and flexibility especially on scenarios that standard AX controls does not solve your problem. You create those controls by creating a new Windows Forms Control Library project from the Visual Studio and referencing it in AX to be used as your managed host control.
We will have an overview on this new functionality by creating a custom control that displays a combo box with a color list in .NET Visual Studio environment and using it on a simple test form to test functionality in Dynamics AX. As you know on some forms in Dynamics AX, there are color selection fields that enable you to assign a color in integer presentation to be used as a background color in grid lines or other places like Gantt chart in Production module :

capture00

When you click on those fields, it opens the legacy Windows color selection box and enable you to select or compose a new color :

capture0

This color selection is then stored in the database by its Integer value. Now imagine we had a customer request to choose and display a color with its readable name instead of integer value displayed like the one in this form, to distinguish better between the colors that look similar to each other. To solve this issue we can use .NET named colors by creating a custom combo box control to select from those named color values. For this blog, I have created a quick and dirty combo box control, that overrides standard .NET ComboBox control and paints a rectangle of color next to it name using the .NET named color list. This example control can be downloaded from my github repository using the link below :

ManagedHostTest.zip

Because in this blog I want to focus on managed host control on Dynamics AX environment, I will not get into the details how to create such a custom control in .NET itself.

capture1

This project is basically a new Windows Forms Control Library project that overrides default combobox and uses a test form to test the control. To be able to import it in Managed Host control, you must sign the form control assembly with a key file. For this purpose you can use the project properties to add a simple key and sign your control with it :

capture12

After that you can test  by running the test form to see that your new control is running ok and export it as a DLL by doing a release build. After that step, we get our form control DLL from the bin\Release folder of our .NET project and copy it in both server\bin and client\bin directories of Dynamics AX :

Capture2.PNG

After that step, we add the DLL file of our control as a reference in the references node of AOT :
capturemh2

Now we are ready to use our new custom control in AX forms. To be able to test it we create a simple AX form (included in the zip file) :

capturemh3

First we add our managedhost control to our form group by right clicking, selecting New Control and ManagetHost from the menu. This will display us a list of custom control assemblies in .NET, here we select our own custom control :

capture35

Similar to other AX controls, we can change simple ax control properties like width, height of our control but we cannot directly change any .NET control properties, or custom properties of our .NET control here. In the following sections I will tell you how to change those. Then we complete our test form by adding two more controls to show the return value, setting their Auto Declaration property to ‘yes’. Now we need to initialize our managed control to a default color on form opening, say it should be ‘White’ on opening. To be able to do that, we can use either the init() method after the call of super() or run() method. In this example I will use the run() method to separate it from the system created code but the same example will work well after the super() call of init method.

public void run()
{
CustomColorCombo.CustomColorComboBox netComboBox;
System.Drawing.Color colorInit;</pre>
super();

//Initialize color combo with white color
colorInit = System.Drawing.Color::get_White();
netComboBox = colorCombo.control() as CustomColorCombo.CustomColorComboBox;
netComboBox.set_SelectedItem(colorInit);
}

Here we parse the .NET control class into our AX code by using the .control() method of the managed host control. Then we are able to change any of the initial properties of our custom control.
And how to subscribe to the events of our custom control? Say we want to display the selected value in our test form after the user clicks our new custom control. To be able to do that we right click our control in the AOT form designer and select Events on the top of the menu. From here we select SelectedIndexChanged event and click Add. This will automatically add an event handler method in our form code, as well as add new lines to class declaration and init() method to subscribe this event :

capturemh4

public class FormRun extends ObjectRun
{
CustomColorCombo.CustomColorComboBox _ColorCombo_Control;
}
public void init()
{</pre>
super();
_ColorCombo_Control = ColorCombo.control();
_ColorCombo_Control.add_SelectedIndexChanged(new ManagedEventHandler(this, 'ColorCombo_SelectedIndexChanged'));

}

Inside the event handler method, we can use the sender method to get an instance to our control and get the selected color value from the combo box :

void ColorCombo_SelectedIndexChanged(System.Object sender, System.EventArgs e)
{
CustomColorCombo.CustomColorComboBox netComboBox = sender as CustomColorCombo.CustomColorComboBox;
Name selectedColor;
int intcolor;</pre>
selectedColor = netComboBox.get_SelectedText();
intcolor = netComboBox.get_SelectedIntColor();

Colorname.text(selectedColor);
intvalue.value(intcolor);

}

Now we are ready to test our form and see our new custom control in action.

capture38

capture5

The integer value coming from the selection of the custom control can be used inside AX form controls as well as inside AX Grid’s display option method. To test it, I want to use the background color property of the group control in our form. For that purpose we need to change some properties of the TestGroup control. We set the ‘BackStyle’ to Opaque and ColorScheme property to RGB, otherwise call to reset the backgroundcolor property will not work :

capture41

Now, if we add the following line at the end of our event handler method we are ready to test the selected color inside our AX form  :

TestGroup.backgroundColor(intcolor);

capture6

Wish you a good fun with this new feature of Dynamics AX 2012!

 

Leave a Reply