Script syntax error

edited 10:48AM in FastReport .NET
Hi!

So, what am I trying to achieve: I would like a TextBox to display different text according to the value assigned to it. When I design the report, the TextBox, lets call it Text1 holds the string "[DataTable.Data]" because it will be populated in realtime from code. I would like it to print, for example "FOO" when the data is "1" and "BAR" when the data is "2". Kinda like the highlight expresion option but with actual text change.

I tried a Text1.AfterPrint event, something like:
private void Text1_AfterPrint(object sender, EventArgs e){
  Text1.Text == "1" ? Text1.Text = "FOO" : Text1.Text = "BAR";
}
But all I got in response was:
5.6.2009 14:49:07 (21,36): error CS1525: Invalid expression term '?'
(21,38): error CS1002:; expected
(21,68): error CS1002:; expected
(21,75): error CS1525: Invalid expression term ':'
(21,77): error CS1002:; expected
(21,109): error CS1002:; expected
Any ideas as to why this doesnt work?

Comments

  • edited 10:48AM
    Hello,

    Text1.AfterPrint is not a good place for this - the object is printed already, all changes in its state will be ignored. Better to use the band.AfterLayout - all objects are filled with data and ready to print.

    The correct code would be:
    private void Text1_AfterPrint(object sender, EventArgs e)
    {
      Text1.Text = Text1.Text == "1" ? "FOO" : "BAR";
    }
    

    But I want to suggest you more elegant solution: just put the following text in your Text object:

    [[DataTable.Data] == "1" ? "FOO" : "BAR"]

    In this case, you don't need the script at all.
  • edited 10:48AM
    ... this code will work if your [DataTable.Data] is of String type. If it is numeric, use the following:

    [[DataTable.Data] == 1 ? "FOO" : "BAR"]
  • edited 10:48AM
    Hi. It works! Thanks, I didnt know you can put expressions inside the [] brackets. Now, forward and upwards!

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.