Recent

Author Topic: Find & Replace  (Read 7049 times)

Giorgio

  • Newbie
  • Posts: 2
    • http://giorgiotani.interfree.it/index.html
Find & Replace
« on: July 13, 2005, 04:29:24 pm »
Hi all, excuse me if it may be a newbye question:
I'm trying to add find and find&replace to a SynEdit.
I've Googled a while and it seem that SearchReplace should do the job, but examples I found on Delphi newsgroup doesn't seem to work in Lazarus, and in the thousands lines of Lazarus editor sourcecode I'm not able to find an example of that, at least not a simple example to understand ad adapt to my very basic "find & replace" need; I also missed to find example code online that exemplify how to do it with Lazarus.
Could someone kindly link or paste here some examples?
Thanks a lot!

RudieD

  • Full Member
  • ***
  • Posts: 234
Find & Replace
« Reply #1 on: July 18, 2005, 08:06:04 pm »
Hi Giorgio,

I've added the following to my form (Hope this helps !):
NB: You have to add the Lazarus path to your projects search path and findreplacedialog to your uses clause !

  TfrmMain = class(TForm)
  private
    { private declarations }
    FFindReplaceDialog: TLazFindReplaceDialog;
    procedure DoFindReplace(Replace:boolean;Again:boolean=False);
    procedure FindAgain;
  public
    { public declarations }
    function FindReplaceDialog:TLazFindReplaceDialog;
  end;

...

procedure TfrmMain.DoFindReplace(Replace:boolean;Again:boolean=False);
var OldCaretXY:TPoint;
  AText,ACaption:AnsiString;
  TopLine,ALeft,ATop: integer;
begin
  if Replace then
    FindReplaceDialog.Options :=
      FindReplaceDialog.Options + [ssoReplace, ssoReplaceAll, ssoPrompt]
  else
    FindReplaceDialog.Options :=
      FindReplaceDialog.Options - [ssoReplace, ssoReplaceAll, ssoPrompt];

  if not Again then
  begin
    FindReplaceDialog.ShowModal;
    if FindReplaceDialog.ModalResult = mrCancel then
      Exit;
    with FindReplaceDialog.TextToFindComboBox do
    begin
      if (Text = '') and (ItemIndex > -1) then
        Text := Items[ItemIndex]
      else
        Text := Text;
        FindReplaceDialog.FindText := Text;
      if Text <> '' then
      begin
        if Items.IndexOf(Text) > 0 then
          Items.Move(Items.IndexOf(Text),0)
        else
          Items.Insert(0,Text);
      end;
    end;
  end;

  OldCaretXY:=SynEdit1.CaretXY;
  if SynEdit1.SelAvail then begin
    if ssoBackwards in FindReplaceDialog.Options then
      SynEdit1.LogicalCaretXY:=SynEdit1.BlockBegin
    else
      SynEdit1.LogicalCaretXY:=SynEdit1.BlockEnd
  end;
  try
    SynEdit1.SearchReplace(
      FindReplaceDialog.FindText,FindReplaceDialog.ReplaceText,FindReplaceDialog.Options);
  except
    on E: ERegExpr do begin
      MessageDlg(lisUEErrorInRegularExpression,
        E.Message,mtError,[mbCancel],0);
      exit;
    end;
  end;
  if (OldCaretXY.X=SynEdit1.CaretX)
  and (OldCaretXY.Y=SynEdit1.CaretY)
  and not (ssoReplaceAll in FindReplaceDialog.Options) then begin
    ACaption:=lisUENotFound;
    AText:=Format(lisUESearchStringNotFound, [FindReplaceDialog.FindText]);
    MessageDlg(ACaption,AText,mtInformation,[mbOk],0);
  end else begin
    TopLine := SynEdit1.CaretY - (SynEdit1.LinesInWindow div 2);
    if TopLine < 1 then TopLine:=1;
    SynEdit1.TopLine := TopLine;
  end;
end;

procedure TfrmMain.FindAgain;
var OldOptions: TSynSearchOptions;
begin
  OldOptions:=[];
  OldOptions:=FindReplaceDialog.Options;
  if ssoEntireScope in OldOptions then
    FindReplaceDlg.Options:=FindReplaceDialog.Options-[ssoEntireScope];
  DoFindReplace(False,True);
  FindReplaceDialog.Options:=OldOptions;
End;

function TfrmMain.FindReplaceDialog: TLazFindReplaceDialog;
begin
  if not assigned(FFindReplaceDialog) then
  begin
    FFindReplaceDialog := TLazFindReplaceDialog.Create(Self);
    FFindReplaceDialog.Position := poOwnerFormCenter;
  end;
  Result := FFindReplaceDialog;
end;
The FRED Trainer. (Training FRED with Lazarus/FPC)

Giorgio

  • Newbie
  • Posts: 2
    • http://giorgiotani.interfree.it/index.html
Find & Replace
« Reply #2 on: July 19, 2005, 08:23:09 am »
Quote from: "RudieD"
Hi Giorgio,
I've added the following to my form (Hope this helps !):

Hi, I'll try it.
For the moment, many thanks for the response!

 

TinyPortal © 2005-2018