Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Answered

Dynamics AX 2012 R3 - change color of forms/controls without hardcoding it

Posted on by Microsoft Employee

Hello everyone

I'm facing following problem:

Forms should be able to have different and custom colors for its controls and the form itself.

This seems to work in some cases, but especially for forms using templates like "ListPage" or "DetailsFormMaster" it does not.

Simply setting it in the properties it not an option, as the colors should be persistent throughout multiple forms and I don't want to change it in every form when I want to change the colors.

I've tried doing several things as I'll describe in a moment, but none worked to my satisfaction.

NOTE:

I based most of my Attempts on information I found on the MSDN.

For example, I can't find it right now, but I read that the colorScheme (and/or backgroundColor) attribute are carried to the children if they use the default values, if you simply change them on the biggest container.

Attempt No 1:

Try to find some sort of property or ini file which stores shared property information for multiple objects. (Did not find one)

Attempt No 2:

Define my own class which the targeted forms could then extend from.

Couldn't find the correct class that I should extend from, as EVERY form is defined like this:

public class FormRun extends ObjectRun
{
}

and I couldn't find anything that would work for me, as extending from ObjectRun didn't work as it resulted in an error (My new class would extend from ObjectRun and the FormRun then would extend from my own, new class).

I did try to extend from the class "Form", which did not result in any errors, but I couldn't override any functions except for "finalize()" I think.

Attempt No 3:

Modify "SysSetupFormRun" to atleast try to change the whole Window Color and try to see if that would work (I found this a lot as people seem to use this to change window colors depending on the current company)

I tried to add this to the "run()" and to the "init()" method, both worked for some windows and dialogs, but forms created from a template still looked mostly the way they always look.

this.design().colorScheme(FormColorScheme::RGB);
this.design().backgroundColor(WinAPI::RGB2int(255, 0, 0));

Attempt No 4:

This is my favorite method so far in terms of code design.

I tried to implement my own small class that simply has a function to change the colors of the "design" thing from the form.

I defined an Enum (WindowColor) to make it easier to choose color and created a table to store the RGB-Values.

public static void changeColor(FormRun fr, WindowColor wc)
{
    ColorDef colorDef; // This is my Table for storing the RGB-Values
    select count(WindowColorId) from colorDef where colorDef.WindowColorId == wc;

    if(colorDef.WindowColorId > 0)
    {
        select colorDef where colorDef.WindowColorId == wc;

        fr.design().colorScheme(FormColorScheme::RGB);
        fr.design().backgroundColor(WinAPI::RGB2int(colorDef.R, colorDef.G, colorDef.B));
    }
    else
    {
        info(strFmt("%1 is not a valid WindowColor", wc));
    }
}


I would simply call this function in the init() function of the desired form and it would work in forms that are NOT based of a template.

public void init()
{
    super();

    ColorChange::changeColor(this, WindowColor::Warning);
}

I want to note, that I also figured out that in the DetailsFormMaster Template the "Style" property of the "design" node is causing this method to NOT work. If you change the style to "auto" the colorchange will work, but the form will lose some of its functionallity (Especially getting spawned in Edit/View mode depending on how its called)


Attempt No 5:
Iterate through all controls of the form recursively and change all controls of the same class.
This kind of worked, only for specific controls though.
I only tested this on a DetailsFormMaster and the only control it would work on was the TabPages.
If I changed the TabPages though the rest of the form would be complete pitch black and I couldn't find a way around that.

Here's the Function I'd call:
public static void changeControlColor(FormBuildControl fbc, WindowColor wc, int ctrlClassNum)
{
    int handle;
    int i;
    FormBuildWindowControl fbwc;

    if(fbc.isContainer())
    {
        for(i = 1; i <= fbc.controlCount(); i++)
        {
            ColorChange::changeControlColor(fbc.controlNum(i), wc, ctrlClassNum);
        }
    }

    handle = fbc.handle();
    if(handle == ctrlClassNum)
    {
        info(strFmt("%1 == %2", fbc.handle(), ctrlClassNum));
        fbwc = fbc;
        fbwc.colorScheme(FormColorScheme::RGB);
        fbwc.backgroundColor(WinAPI::RGB2int(255, 0, 0));
    }
}
and this is how I called it (in init()):

int i;
    
for(i = 1; i <= element.form().design().controlCount(); i++)
{
    ColorChange::changeControlColor(element.form().design().controlNum(i), WindowColor::Important, classNum(FormBuildGridControl));
}

--------

Ok I think thats about everything I tried after hours of research.
If I missed anything or you have any questions regarding my Attemps feel free to ask.

I hope anyone here is able to help me.
So please if you know anything about this, whether this doesn't even work or I'm simply trying to do this compeletly wrong, tell me about it.
Also if you could think of anything that maybe doesn't solve this problem exactly but might help me get a similiar result, feel free to tell me about it.


Thank you very much in advance

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Dynamics AX 2012 R3 - change color of forms/controls without hardcoding it

    Thank you for your answer!

    Its a pitty that ListPages don't support coloring.

    The tip with the bar on the top is very great though, thanks alot!

  • Verified answer
    Alex Kwitny Profile Picture
    Alex Kwitny 395 on at
    RE: Dynamics AX 2012 R3 - change color of forms/controls without hardcoding it

    You aren't very clear on what your actual question is. If you want colors on all forms, you'll have trouble with ListPages, but if you're looking for an indicator, you should look at adding a bar at the top.

    See this post for a great example:

    daxguy.blogspot.com/.../ax-environment-information-on-ui-simple.html

Helpful resources

Quick Links

Dynamics 365 Community Update – Sep 9th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,246 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,041 Super User 2024 Season 2

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans