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!
FastReport® VCL - is an add-on component that allows your application to generate reports quickly and efficiently. FastReport® provides all the necessary tools to develop reports, including a visual report designer, a reporting core, and a preview window.
FastReport provides free open source report generator for . NET 6/. NET Core/. NET Framework.
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;
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;
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With