Rave Language: Highlighting Text

Category

Rave - General

Question

How can I display text with different colors based on a value from my database?

Solution

This can be very easily done using the Rave Language. For this example I created a simple table using DBDemos and selected the biolife table. What I want to do is highlight all fish from the Shark category by changing the color to red and the Snapper category I want printed using Navy. All other categories of fish I want printed in green. This would be done with the following code, which would be placed in the OnBeforePrint event of the DataText component:

if Compare(DataView1Category.AsString,'Shark') = 0 then
  self.Color := clRed;
  self.Left := self.Left + 0.1;
elseif Compare(DataView1Category.AsString,'Snapper') = 0 then
  self.Color := clNavy;
  self.Left := self.Left - 0.1;
else
  self.Color := clGreen;
end if;

Just for kicks, I also decided to indent the shark category and unindent the snapper category of fish. Now, you might be looking at this code and thinking that it has a bug in it. "What happens if you get two or three fish from the shark category? Each one would be indented/unindented a little more and would potentially run into another column or off the page." The reason the above code is correct and actually does line things up correctly is that before each component is printed its designed position is saved off. Then after it is printed the designed position is restored. This means that changes to the position of the component are only temporary.