Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi Find and Replace.

Posted on 1999-07-19
8
Medium Priority
?
4,586 Views
Last Modified: 2008-02-20
How do I make a find and replace text in a memo field, just like Borlands Delphi? Please give examples. Thank's Hyperion66
0
Comment
Question by:hyperion66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 

Author Comment

by:hyperion66
ID: 1389561
Edited text of question.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389562
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
0
 

Author Comment

by:hyperion66
ID: 1389563
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389564
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
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1389565
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

0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1200 total points
ID: 1389566
hi hyperion66,

just a preventive lock,
because sometimes happens to me
that a q becomes deleted.

reject, if my advise not helped you

meikl ;-)
0
 

Author Comment

by:hyperion66
ID: 1389567
Hi, kretzschmar

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

Author Comment

by:hyperion66
ID: 1389568
Thank's kretzschmar.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question