Graphics

 View Only
  • 1.  Changing Text Color based on Positive or Negative

    Posted 24 days ago

    Hello,

    I want to have a text object change to Red or Green depending on if the number inputted is negative or positive. I tried using visual logic but it doesn't like (-) or (+) and seems to break when the number contains a comma.

    I tried to create a script for it:

    dim txtObj as xpTextObject
    dim numValue as double
    
    Self.getObjectByName("DOW JONES CHANGE", txtObj)
    
    numValue = cDbl(txtObj.Text)
    
    If numValue > 0 then
    	txtObj.setMaterial(0, "STOCKS GREEN")
    
    elseIf numValue < 0 then
    	txtObj.SetMaterial(0, "STOCKS RED")
    
    else
    	txtObj.SetMaterial(0, "WHITE")
    
    end If

    It compiles correctly but doesn't change the text object it's attached to.

    Thanks in advance!



    ------------------------------
    Darren Johnson
    Director
    WFFT
    ------------------------------


  • 2.  RE: Changing Text Color based on Positive or Negative

    Posted 23 days ago

    Hi Darren. 

    Use a script below.

    "{M:WHITE}" attaches the material to the text object (in my opinion works only v.10 and higher, in м.7 it definitely doesn't work)

            Dim txtObj As xpTextObject
            Dim numValue As Double
            self.GetObjectByName("DOW JONES CHANGE", txtObj)
            If Double.TryParse(txtObj.Text, numValue) Then 'the script does not stop if the text is not a number
                If numValue > 0 Then
                    txtObj.Text = "{M:STOCKS GREEN}" & txtObj.Text
                ElseIf numValue < 0 Then
                    txtObj.Text = "{M:STOCKS RED}" & txtObj.Text
                Else
                    txtObj.Text = "{M:WHITE}" & txtObj.Text
                End If
            End If
    And if the script doesn't work, try using the script below. You will need to create three fonts with three colors: white, red and green. The script applies the corresponding font to the text object
            Dim txtObj As xpTextObject
            Dim numValue As Double
            self.GetObjectByName("DOW JONES CHANGE", txtObj)
            Dim temptxt as String = txtObj.Text
            txtObj.Text = ""
            If Double.TryParse(temptxt, numValue) Then
                Dim font As xpFont
                If numValue > 0 Then
                    self.Project.GetFontByName("STOCKS GREEN", font)
                ElseIf numValue < 0 Then
                    self.Project.GetFontByName("STOCKS RED", font)
                Else
                    self.Project.GetFontByName("WHITE", font)
                End If
                txtObj.CurrentFont = font
            End If
            txtObj.AppendToLine(0, temptxt)



    ------------------------------
    Roman Yaroshenko
    chief specialist
    RBC-TV
    Moscow Russian Federation
    ------------------------------



  • 3.  RE: Changing Text Color based on Positive or Negative

    Posted 23 days ago

    Hi Darren

    Put this code into the OnSetText method of the text object.

    It assumes the text is white to start with, so just changes it to red or green using font tags

    Simon

    if val(text)=0 then exit sub
    text = iif(val(text)>0,"{M:Stocks Green}","{M:Stocks Red}") + text


    ------------------------------
    Simon Latus
    Simon Latus Media Ltd
    United Kingdom
    ------------------------------



  • 4.  RE: Changing Text Color based on Positive or Negative

    Posted 23 days ago

    Thanks for the quick responses!

    I got Simon's to work. I appreciate the help.



    ------------------------------
    Darren Johnson
    Director
    WFFT
    ------------------------------