Reputation: 139
I have a memo object in my report, and a need replace "%...%" strings. For example, in Rave Report:
MemoBuf.ReplaceAll('%my_str%', "new string", false);
But, don't exists a method (or property) to replace text, in the FastReport
. How I can do this?
I'm using Fast Report 4.9.72
and Delphi 2010
.
Thanks!
Upvotes: 5
Views: 29920
Reputation: 1921
you can use this code in fast report:
function StringReplace(const S, OldPattern, NewPattern: string;
iReplaceAll: boolean=true; iIgnoreCase :boolean=true): string;
var
SearchStr, Patt, NewStr: string;
Offset: Integer;
begin
if iIgnoreCase then begin
SearchStr := UpperCase(S);
Patt := UpperCase(OldPattern);
end else begin
SearchStr := S;
Patt := OldPattern;
end;
NewStr := S;
Result := '';
while SearchStr <> '' do begin
Offset := Pos(Patt, SearchStr);
if Offset = 0 then begin
Result := Result + NewStr;
Break;
end;
Result := Result + Copy(NewStr, 1, Offset - 1) + NewPattern;
NewStr := Copy(NewStr, Offset + Length(OldPattern), MaxInt);
if not iReplaceAll then begin
Result := Result + NewStr;
Break;
end;
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
end;
end;
Upvotes: 2
Reputation: 76693
Since there is no StringReplace
in FastReport available I would do it from the Delphi code. It is possible to import functions somehow but this seems to me better arranged. Please note, that in this first example I suppose that the Memo1
exists (you would get an access violation otherwise).
procedure TForm1.Button1Click(Sender: TObject);
var
Memo: TfrxMemoView;
begin
Memo := frxReport1.FindObject('Memo1') as TfrxMemoView;
Memo.Text := StringReplace(Memo.Text, '%my_str%', 'new string', [rfReplaceAll]);
frxReport1.ShowReport;
end;
If you are not sure about component name or type you should use something like this:
procedure TForm1.Button2Click(Sender: TObject);
var
Memo: TfrxMemoView;
Component: TfrxComponent;
begin
Component := frxReport1.FindObject('Memo1');
if Component is TfrxMemoView then
begin
Memo := Component as TfrxMemoView;
Memo.Text := StringReplace(Memo.Text, '%my_str%', 'new string', [rfReplaceAll]);
frxReport1.ShowReport;
end;
end;
Upvotes: 7
Reputation: 25678
I do not know what the purpose of that code was in Rave Reports as I never used it, but I can suggest alternatives for FastReport:
[my_str]
. This is probably the best option. The content of those brackets is actually a full blown pascal expression that can make use of dataset fields, report variables, registered functions. You could even write a Delphi function, register it with FastReport and call it from within the [..]
, passing a field from the dataset as a parameter. The possibilities really are endless.Upvotes: 1