Help

I want to have a field change its fill color based on the number of another field. If you were spending points on something and your points hit zero then a field turns yellow. Thanks for any help.

Comments

  • edited December 2019

    Here is two ways to do it with Scripts.

    First

    This variant uses entire field objects as argument to a function
    Add the following script to the Script page

    function ColorValue(fieldToColor,fieldWithValue)
        {
          try
          {
            if(fieldWithValue.Value<=0)
            {
              fieldToColor.FillColor= rgb(255, 255, 0);     // yellow
            }
            else
            {
              fieldToColor.FillColor= rgb(0, 0, 0);     // black
            }
           // regardsless of actual value, return the value to show the output
           return  fieldWithValue.Value;
          }
          catch(e)
          {
              debugmsg(e.message);
          }
          finally
          {
             // do nothing
          }
        }
        // Support function
        function rgb( r, g, b)
        {
            return r + (256 * g) + (65536 * b);
        }

    And in the source of the field(named txtValueToColor in this example) you want to color, set the follwing
    js:ColorValue(txtValueToColor,txtValue)

    Another way to do this

    This variant adds a document wide function that runs if ANY content in the document gets update. Needless to say, the first variant is to be prefered but sometimes this comes in handy.
    Add an event for PostUpdate in Document Setup

    Then in add to the Scripts page

    function OnPostUpdate()
        {
          try
            {    
            if (txtValue.Value<=0) 
              {
              txtValue.FillColor = rgb(255, 255, 0);     // yellow
              shpIndicator.FillColor = rgb(255, 255, 0); // yellow
              shpIndicator.LineColor = rgb(255, 0, 0); // red
              }
              else
              {
              // use normal color
              txtValue.FillColor = rgb(0, 0, 0); // black
              shpIndicator.FillColor = rgb(255, 255, 255); // white
              shpIndicator.LineColor = rgb(0,0, 0); // black
              }
            }
          catch(e)
          {
              debugmsg(e.message);
          }
          finally
          {
             // do nothing
          } 
        }

    Edited 2019-12-02 14:30:00 reason:
    Forgot to output value from function ColorValue

  • Wow,thanks. Your the best.

  • Your welcome, I made a small update to script, returned value from ColorValue, see post

Leave a Comment