C++ Builder 6 添加变量到 Fastreport 4

这几天一直在找C++ Builder 6 代码中的变量 传递到 FastReport 4 中的方法。找来找去没找到完全的方法,最后到了FastReport官网下了FR4.6 Programmer Manual 问题得以解决。其实也蛮方便的,只不过没用过不知道如何着手。

下面是方法:

Fastreport 控件 TfrxReport 名称 frxReport;
1.添加变量(变量类型是 Variant万能变量 )到脚本
frxReport->Script->Variables["变量名"] = 值;
如:定量变量 type ,然后 赋值为 1
frxReport->Script->Variables["type"] = 1;

然后在 双击frxReport控件后的报表设计窗的Code里面就可以直接使用变量type了。

如需在代码中多次修改type的值可以多次使用上面的语句,只需将值修为要赋给type的值即可。
而且必须在报表预览前运行过次语句,否则会在预览时报出type未定义的错误。
在Fastreport 4.0 中的脚本不能预先定义type,否则会报重复定义。但是在fastreport网站下载的说明里面提到若该变量存在可以是对该变量赋值,可能4.0的版本中还未实现。

FR4.6 Programmer Manual中的原文:
    Instead of report variables, script variables are in the TfrxReport.Script. You can define them using FastScript methods. Let's look at some differences between report and script variables::
 

 

Report variables

Script variables

Placement

In the report variables list, TfrxReport.Variables.

In the report script, TfrxReport.Script.Variables.

Variable name

May contain any symbols.

May contain any symbols. But if you want to use that variable inside the report script, its name should conform to Pascal identificator specifications.

Variable value

May be of any type. Variables of string type are calculated each time you access them, and are, in itself, an expressions.

May be of any type. No calculation is performed, behavior is like standard language variable.

Accessibility

Programmer can see the list of report variables in the "Data tree" window.

The variable is not visible, programmer should know about it.

       Working with script variables is easy. Just assign value to the variable this way:

 

Pascal:

 

frxReport1.Script.Variables['My Variable'] := 'test';

 

C++:

 

frxReport1->Script->Variables->Variables["My Variable"] = "test";

 

       In this case FastReport will create a variable if it is not exists, or assign a value to it. There is no need to use extra quotes when assigning a string to that variable.

   

2.添加变量到Fastreport报表
首先必须添加一个Category,才能向报表添加变量。
进入报表设计界面,点击菜单栏的“报表”--“变量”,然后在弹出的窗口上面点击工具栏的第一个图标,新建一个Category,然后点击最后一个图标“√”,完成Category的添加。

1)
frxReport->Variables->Variables["变量名"] = 值;
与frxReport->Script->Variables["变量名"] = 值是相似的,使用参考上面的添加变量到脚本。

2)
TfrxVariable * Variable;

Variable = frxReport->Variables->Add();
Variable->Name = "变量名";
Variable->Value = 值;

上面两种方法都是将变量添加到最后一个Category,如果想添加到特定的Category可以使用
Variable = frxReport->Variables->Insert(1);
代替 Variable = frxReport->Variables->Add();
也可以使用下面的方法
3)
添加到已特定的Category,在上面的添加Category如果添加了多个,可以将变量添加到指定的Category

frxReport->Variables->AddVariable("My Category", "变量名", 值);

My Category 为Category的名称

这种方法在脚本中脚本中使用 变量必须加上 "< >"符号使用该变量。
如:增加的变量为  frxReport->Variables->Variables["type"] = 1;
在脚本中 使用 type变量 为 <type>,就是将其视为数据库中的一个字段


自己描述总觉得啰啰嗦嗦,FR4.6 Programmer Manual中的原文:
Variables can be added only after a category is already added. All the variables located in the list after the category, are considered belonging to this category. Variables’ names must be unique within the whole list, and not within a category

 

There are several ways to add a variable to the list:

 

Pascal:

 

frxReport1.Variables['My Variable 1'] := 10;

 

C++:

 

frxReport1->Variables->Variables["My Variable 1"] = 10;

 

this way adds a variable (if it does not exist already) or modifies a value of the existing variable.

 

Pascal:

 

var

Variable: TfrxVariable;

 

Variable := frxReport1.Variables.Add;

Variable.Name := 'My Variable 1';

Variable.Value := 10;

 

C++:

 

TfrxVariable * Variable;

 

Variable = frxReport1->Variables->Add();

Variable->Name = "My Variable 1";

Variable->Value = 10;

 

Both of the ways add a variable to the end of the list, therefore, it would be added to the last category. If a variable is supposed to be added to a specified position of the list, use the “Insert” method:

 

Pascal:

 

var

Variable: TfrxVariable;

 

Variable := frxReport1.Variables.Insert(1);

Variable.Name := 'My Variable 1';

Variable.Value := 10;

 

C++:

 

TfrxVariable * Variable;

 

Variable = frxReport1->Variables->Insert(1);

Variable->Name = "My Variable 1";

Variable->Value = 10;

 

If a variable is to be added to the specified category, use the “AddVariable” method:

 

Pascal:

 

frxReport1.Variables.AddVariable('My Category 1', 'My Variable 2', 10);

 

C++:

 

frxReport1->Variables->AddVariable("My Category 1", "My Variable 2", 10);

 

 

你可能感兴趣的:(C++ Builder 6 添加变量到 Fastreport 4)