Link to home
Start Free TrialLog in
Avatar of hyperion66
hyperion66

asked on

Delphi Find and Replace.

How do I make a find and replace text in a memo field, just like Borlands Delphi? Please give examples. Thank's Hyperion66
Avatar of hyperion66
hyperion66

ASKER

Edited text of question.
Avatar of kretzschmar
hi hyperion66,

what does you exactly mean ?

a sample how to use the find and replace dialog

unit f_r_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    ReplaceDialog1: TReplaceDialog;
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
    procedure ReplaceDialog1Close(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var
  oldpos : integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  oldpos := memo1.selstart;
  Replacedialog1.FindText := copy(Memo1.text,memo1.selstart+1,memo1.sellength);
  ReplaceDialog1.Execute;
end;

Function StrUp (S : String) : String;
var i : Integer;
begin
  for i := 1 to length(s) do s[i] := UpCase(s[i]);
  result := s;
end;


procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  FCase : Integer;
  SelTmp: Integer;
  SelPos: Integer;
begin
  if frmatchcase in replacedialog1.Options then
    fCase := 1
  else fcase := 0;
  if Memo1.sellength > 0 then
    Selpos := Memo1.SelStart + Memo1.SelLength
  else
    SelPos := Memo1.SelStart;
  case fCase of
    0 : Begin
          if selpos = 0 then
            Selpos := pos(StrUp(ReplaceDialog1.FindText),StrUp(memo1.text))
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(StrUp(ReplaceDialog1.FindText),copy(StrUp(memo1.text),selpos,length(memo1.text)- selpos )) - 1;
            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
    1 : Begin
          if selpos = 0 then
            Selpos := pos(ReplaceDialog1.FindText,memo1.text)
          else
          begin
            SelTmp := Selpos;
            Selpos := pos(ReplaceDialog1.FindText,copy(memo1.text,selpos,length(memo1.text)- selpos )) - 1;
            if selpos > 0 then selpos := selpos + seltmp;
          end;
        end;
  end;
  if SelPos > 0 then
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(ReplaceDialog1.FindText);
  end
  else MessageDlg(ReplaceDialog1.FindText + ' not found in Memo1', mtError, [mbOk], 0);
  memo1.setfocus;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  if Memo1.sellength > 0 then
    if frmatchcase in replacedialog1.Options then
    begin
      if Replacedialog1.Findtext = copy(memo1.text,memo1.selstart+1,memo1.sellength) then
      begin
        Memo1.SelText := ReplaceDialog1.ReplaceText
      end
    end
    else
      if StrUp(Replacedialog1.Findtext) = StrUp(copy(memo1.text,memo1.selstart+1,memo1.sellength)) then
         Memo1.SelText := ReplaceDialog1.ReplaceText;
  Replacedialog1find(self)
end;

procedure TForm1.ReplaceDialog1Close(Sender: TObject);
begin
  memo1.selstart := oldPos;
  memo1.sellength := 0;
end;

end.

meikl
I want to make a find and replace text in a memo filed just like the one in borland delphi. When you search for text in code or a text file no matter upper case or lower case, Delphi's find always finds the text. and a replace dialog to do the same.
hi hyperion66,

check out my sample above ;-)
it does it also
if you mark the checkbox in the dialog

of course not all features are implemeted here
but the caseinsensitivity is implemented

meikl
hi hyperion66,

an other method, but works only with a richedit not with a memo
its full featured with the default find and replace dialog

(MatchCase or not, WholeWord or not, Replace all or single)


unit m_f_r_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, RichEdit, ComCtrls;

type
  TForm1 = class(TForm)
    ReplaceDialog1: TReplaceDialog;
    Button1: TButton;
    Memo1: TRichEdit;
    procedure ReplaceDialog1Close(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Show(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

var
  FoundAt : Integer;
  FT : TFindText;
  OldS, OldSL : Integer;

{$R *.DFM}

procedure TForm1.ReplaceDialog1Close(Sender: TObject);
begin
  Memo1.SelStart := OldS;
  Memo1.SelLength := OldSL;
end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.FindText := Memo1.SelText;
  ReplaceDialog1.Execute;
end;

procedure TForm1.ReplaceDialog1Show(Sender: TObject);
begin
  OldS := Memo1.SelStart;
  OldSL := Memo1.SelLength;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  MatchC, WholeW : Integer;
  fs : String;
begin
  fs := ReplaceDialog1.FindText;
  FT.chrg.cpMin := Memo1.SelStart;
  FT.chrg.cpMax := Length(Memo1.Text);
  FT.lpstrText := PChar(fs);
  If frWholeWord in ReplaceDialog1.Options then
    WholeW := FT_WHOLEWORD
  else
    WholeW := 0;
  If frMatchCase in ReplaceDialog1.Options then
    MatchC := FT_MATCHCASE
  else
    MatchC := 0;
  FoundAt := Memo1.Perform(EM_FindText,MatchC or WholeW,integer(@FT));
  if FoundAt > -1 then
  begin
    memo1.SelStart := FoundAt;
    memo1.SelLength := length(fs);
    If frReplaceAll in ReplaceDialog1.Options then
      ReplaceDialog1Replace(Self);
  end
  else  ShowMessage(fs+' is never found');
  memo1.SetFocus;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  If (FoundAt = Memo1.SelStart) and
     (Memo1.SelLength = Length(ReplaceDialog1.Findtext)) then
    Memo1.SelText := ReplaceDialog1.Replacetext;
  ReplaceDialog1Find(Self);
end;

end.

meikl

ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, kretzschmar

I will get back to you sometime today, i've been vary busy with work. Thank's hyperion66.
Thank's kretzschmar.