ENTERPRISE

Microsoft Dynamics AX 2009 : Building Lookups - Picking a color

12/2/2012 6:32:56 PM
In this recipe, we will be creating a color lookup. As an example, we will create the possibility to choose a color in the Company information form, which might be used to define different colors for each Dynamics AX company. That's one of my favorites and is very useful in Dynamics AX multi-company installations. Normally, I use additional code, which reads the user setting and changes the background color of all forms in the same company. This allows users to easily identify in which company they are and prevents incorrect postings.

How to do it...

  1. 1. Open the CompanyInfo table in AOT, and create a new field with properties:

    Property Value
    Type Integer
    Name CompanyColor
    ExtendedDataType CCColor

  1. 2. Open the CompanyInfo form in AOT, and add a new IntEdit control into the TopPanel group right after the GroupName group. Set the following properties:

    Property Value
    Name CompanyColor
    AutoDeclaration Yes
    Label Company color
    LookupButton Always
    ShowZero No
    ColorScheme RGB

  1. 3. Create a new edit method in the CompanyInfo form data source:

    edit CCColor editCompanyColor(
    boolean _set,
    CompanyInfo _companyInfo,
    CCColor _color)
    
    {;
    if (_companyInfo.CompanyColor)
    {
    CompanyColor.backgroundColor(
    _companyInfo.CompanyColor);
    }
    else
    {
    CompanyColor.backgroundColor(
    WinAPI::RGB2int(255,255,255));
    }
    return 0;
    }
    
  2. 4. Set the name of the newly created method in the DataMethod property of the previously created CompanyColor control:

    Property Value
    DataSource CompanyInfo
    DataMethod editCompanyColor

  1. 5. And override its lookup() method with:

    public void lookup()
    {
    int red;
    int green;
    int blue;
    container color;
    color lookupcolor lookupcreating, steps;
    [red, green, blue] = WinApi::RGBint2Con(
    CompanyColor.backgroundColor());
    color = WinAPI::chooseColor(
    element.hWnd(),
    red,
    green,
    blue,
    null,
    true);
    if (color)
    {
    [red, green, blue] = color;
    CompanyInfo.CompanyColor = WinAPI::RGB2int(
    red,
    green,
    blue);
    CompanyColor.backgroundColor(
    CompanyInfo.CompanyColor);
    }
    }
    
    
    					  
  2. 6. In the end the form should look like this in AOT:

  1. 7. To test the results, open Basic | Setup | Company information, and click on the Company color lookup:

How it works...

Colors in Dynamics AX are stored as integers, so first we create a new Integer field. The rest of the code needs to be added to the form itself.

On the form we create the IntEdit control, which represents the company color selection. We have to force the appearance of the lookup button by setting the LookupButton property to Always. We also need to set ColorScheme to RGB to make sure the control allows us to set its color as an integer.

Next, we create the edit method, which is bound to the created control. It is only responsible for setting the control's background to match the stored color. The background is set to white, if no value is present. The method always returns 0, because we do not want to show color code in it. The control's ShowZero property is set to No to ensure that even 0 is not displayed. In this way, we create a control that looks like a real color selection control although Dynamics AX does not have a standard one. The method is bound to the control by specifying its name in the control's DataMethod property.

The last thing to do is to override the control's lookup() method with the code that invokes the color picker. Here we use RGBint2Con() method of WinAPI application class to retrieve the current control's background color, which is passed to chooseColor() of WinAPI to make sure that current color is preselected on the lookup. Note that numeric color representation here is converted into red, green, and blue color components by the use of RGBint2Con(). The chooseColor() is the main method, which builds the lookup. It accepts the following arguments:

  1. 1. Current window handle.

  2. 2. Red color component.

  3. 3. Green color component.

  4. 4. Blue color component.

  5. 5. A binary object representing up to 16 custom colors.

  6. 6. Defining if the lookup is initially expanded.

This method returns a container of red, green, and blue color components, which has to be converted to a numeric value in order to store it in the table field.

There's more...

You probably have noticed that the fifth argument in the example above is set to null. This is because we did not use custom colors. This feature is not that important but may be used in some circumstances.

We will update the lookup() method with additional code to implement custom colors:

public void lookup()

{
int red;
int green;
int blue;
container color;
Binary customColors;
;
customColors = new Binary(64);
customColors.byte(0,255);
customColors.byte(1,255);
customColors.byte(2,0);
customColors.byte(4,0);
customColors.byte(5,255);
customColors.byte(6,0);
customColors.byte(8,255);
customColors.byte(9,0);
customColors.byte(10,0);
[red, green, blue] = WinApi::RGBint2Con(
CompanyColor.backgroundColor());
color = WinAPI::chooseColor(
element.hWnd(),
red,
green,
blue,
customColors,
true);
if (color)
{
[red, green, blue] = color;
CompanyInfo.CompanyColor = WinAPI::RGB2int(
red,
green,
blue);
CompanyColor.backgroundColor(
CompanyInfo.CompanyColor);
}
}


					  

Here, we define the variable customColors as a Binary object for storing initial custom colors. The object structure contains 64 elements for storing color codes. The set of red, green, and blue color components are stored to every three object elements in sequence with a single space between each set. In the previous code, we store yellow (red = 255, green = 255, and blue = 0) in elements from 0 to 2, green (red = 0, green, = 255, blue = 0) from 4 to 6, and red (red = 255, green = 0, blue = 0) from 8 to 10. The system allows creating up to 16 custom colors. Note that the color lookup now looks a bit different:

Custom colors can be modified by the user and can be stored in the table field or cache for later use by storing the whole binary customColors object.
Other  
  •  Microsoft Dynamics AX 2009 : Building Lookups - Selecting a file
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 3) - Sponsorship Management
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 2) - Providing a Sponsor, Leasing and Remote Activation Modes
  •  Programming .NET Components : Remoting - Leasing and Sponsorship (part 1) - Lease Properties, Configuring a Lease, Renewing a Lease
  •  DisplayPort 1.2 - Meet HDMI’s Smarter Brother
  •  HP ProLiant Servers AIS : Processors and Multiprocessing - The Processor Performance Evolution
  •  HP ProLiant Servers AIS : Processors and Multiprocessing - How Processors Work
  •  How To Get The Best From Your Batteries (Part 2)
  •  How To Get The Best From Your Batteries (Part 1)
  •  The 30 Most Important Technology Trends (Part 3)
  •  
    Video
    PS4 game trailer XBox One game trailer
    WiiU game trailer 3ds game trailer
    Top 10 Video Game
    -   The Elder Scrolls Online | Creating a Character on the console version
    -   Hunger Games: Mockingjay Part 2 | First Look
    -   Final Fantasy XV Episode Duscae | Version 2.0
    -   Mirror's Edge Catalyst
    -   Call of Duty: Black Ops 3 Reveal-Trailer
    -   LEGO Marvel’s Avengers
    -   Dark Souls 3 (PS4, Xbox One)
    -   The Last Guardian - E3
    -   Final Fantasy 15: Episode Duscae | 'It Takes Two' Sidequest
    -   Armikrog [PC] Beak-Beak's Blog
    -   Project CARS [WiiU/PS4/XOne/PC] Racing Icon Car Pack
    -   Sonic Boom: Fire & Ice [3DS] Debut