Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can replace strings in a memo (FastReport)?

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!

like image 206
André Avatar asked Feb 10 '12 11:02

André


People also ask

What is fast report?

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.

Is fast report free?

FastReport provides free open source report generator for . NET 6/. NET Core/. NET Framework.


3 Answers

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;
like image 149
TLama Avatar answered Oct 02 '22 04:10

TLama


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;
like image 20
MohsenB Avatar answered Oct 02 '22 02:10

MohsenB


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:

  • In fast report all Memo's support variable substitution. Set up a report variable named "my_str", edit your Memo and include the variable. The text in the memo would look like this: [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.
  • FastReport memos can be manipulated from the Delphi side, so you can use whatever function you want to change the Text of the memo.
  • Memos can also be manipulated from the project's script (from within the report, not from Delphi), you can also do whatever you need to do.
like image 31
Cosmin Prund Avatar answered Oct 02 '22 04:10

Cosmin Prund