首页 > 代码库 > FastReport 变量列表使用
FastReport 变量列表使用
使用报表变量时,引用“frxVariables”单元。 变量定义在“TfrxVariable” 类:
TfrxVariable = class(TCollectionItem)
published
property Name: String; //Name of a variable
property Value: Variant; //Value of a variable
end;
变量列表在“TfrxVariables” 类,有所有相关的方法:
TfrxVariables = class(TCollection)
public
function Add: TfrxVariable; //Adds a variable to the end of the list
function Insert(Index: Integer): TfrxVariable; //Adds a variable to the given position of the list
function IndexOf(const Name: String): Integer; //Returns the index of a variable with the given name
procedure AddVariable(const ACategory, AName: String; const AValue: Variant); //Adds a variable to the specified category
procedure DeleteCategory(const Name: String); //Deletes a category and all its variables
procedure DeleteVariable(const Name: String); //Deletes a variable
procedure GetCategoriesList(List: TStrings; ClearList: Boolean = True); //Returns the list of categories
procedure GetVariablesList(const Category: String; List: TStrings); //Returns the list of variables in the specified category
property Items[Index: Integer]: TfrxVariable readonly; //The list of variables
property Variables[Index: String]: Variant; default; //Values of variables
end;
如果变量的列表很长,可以按类别分组。例如,当有下列变量列表:
Customer name
Account number
in total
total vat
可以使用以下方式:
Properties
Customer name
Account number
Totals
In total
total vat
有以下局限:
- 必须创建至少一个类别
- 类别在data tree下层, 变量在第二层
- 类别不能嵌套
- 变量的名称必须是唯一的,在整体列表中,而不是在一个类别中
一:创建一个变量列表
A link to the report variables is stored in the “TfrxReport.Variables” property. To create a list manually, the following steps must be performed:
- clear the list
- create a category
- create variables
- repeat the 2 and 3 steps to create another category
二:清空变量列表
It is performed with the help of the “TfrxVariables.Clear” method:
frxReport1.Variables.Clear;
三:添加分类
必需至少创建一个类别, 类别和变量保存在一个list。 类别不同于变量是以一个空格开始,这是名字的第一个符号.所有的变量都是属于这一类别。
有两个方法添加类别:
frxReport1.Variables[‘ ‘ + ‘My Category 1‘] := Null;
or
var
Category: TfrxVariable;
Category := frxReport1.Variables.Add;
Category.Name := ‘ ‘ + ‘My category 1‘;
四:添加变量
添加变量必须在类别添加以后, 在列表中变量名必须唯一, 并且必须在类别中。
这里有几个方法添加变量到列表:
frxReport1.Variables[‘My Variable 1‘] := 10; // 添加(如果不存在) 或修改一个变量的值。
var
Variable: TfrxVariable;
Variable := frxReport1.Variables.Add;
Variable.Name := ‘My Variable 1‘;
Variable.Value := 10;
以上2个方法都把变量添加到列表最后, 因此, 添加到最后的类别. 如果想添加到列表的指定位置,使用“Insert” 方法:
var
Variable: TfrxVariable;
Variable := frxReport1.Variables.Insert(1);
Variable.Name := ‘My Variable 1‘;
Variable.Value := 10;
添加到指定的类别,使用“AddVariable” 方法:
frxReport1.Variables.AddVariable(‘My Category 1‘, ‘My Variable 2‘, 10);
五:删除变量
frxReport1.Variables.DeleteVariable(‘My Variable 2‘);
六:删除分类
frxReport1.Variables.DeleteCategory(‘My Category 1‘);
七:修改变量值
八:脚本变量
你可以在TfrxReport.Script中定义脚本变量,用来代替report变量, 看看 report变量和 script变量的不同:
Report variables | Script variables | |
位置 | 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. |
可访问性 | 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:
frxReport1.Script.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.
九:TfrxReport.OnGetValue中传递变量值
最后一种传递值到报表中的方法是使用 TfrxReport.OnGetValue 事件,这个方式可以得到动态值,以前的方法通过静态值。
举例说明使用方法. 报表中放一个文本对象,输入以下内容:
[My Variable]
创建 TfrxReport.OnGetValue 事件:
procedure TForm1.frxReport1GetValue(const VarName: String; var Value: Variant);
begin
if CompareText(VarName, ‘My Variable‘) = 0 then
Value := ‘test‘
end;
运行报表,我们看到变量是显示正确的。 事件TfrxReport.OnGetValue 在遇到所有未知变量时调用 。
http://www.cnblogs.com/moon25/p/5534095.html
FastReport 变量列表使用