使用報表變量時,引用“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