Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how do i find text, find next  and replace text?

Posted on 1998-12-10
8
Medium Priority
?
880 Views
Last Modified: 2010-04-04
how do i find text, find next and replace text in a memo field??? and not have the search case sensitive?
0
Comment
Question by:hyper66
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
BoRiS earned 450 total points
ID: 1350357
hyper66

How thing going over there, long time no hear...here is some simple code to search a memo and replace it with other text...

procedure TForm1.Button2Click(Sender: TObject);

var
  i : integer;
  s1 : string;
  SearchStr : string;
  NewStr : string;
  place : integer;
begin
  SearchStr := 'line';
  NewStr := 'OneEye';
  for i := 0 to Memo1.Lines.Count -1 do begin
    s1 := Memo1.Lines[i];
    Repeat
      Place := pos(SearchStr, s1);
      if place > 0 then begin
        Delete(s1, Place, Length(SearchStr));
        Insert(NewStr, s1, Place);
        Memo1.Lines[i] := s1;
      end; //if-then
    until place = 0;
  end; //for-loop
end;

if you need some more help just leave a comment...

Later
BoRiS
0
 

Author Comment

by:hyper66
ID: 1350358
Hi Boris, It's been along time...Cold and cloudy for the most part..what is the url to your new webpage?

Sorry boris i did'nt explain what i was looking for good enough.. What i ment find and replace and find next useing Find dialog and replace dialog..
0
 

Author Comment

by:hyper66
ID: 1350359
Adjusted points to 150
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:BoRiS
ID: 1350360
hyper66

Ok, here is some code to use the find/replace dialog to find text and replace it...

procedure TForm1.Button1Click(Sender: TObject);
begin
  ReplaceDialog1.Position := Point(Memo1.Left + Memo1.Width, Memo1.Top);
 ReplaceDialog1.Execute;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with Memo1 do
  begin
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }
    if SelLength <> 0 then
      StartPos := SelStart + SelLength;
    else
      StartPos := 0;
    { ToEnd is the length from StartPos to the end of the text in the rich edit control }
    ToEnd := Length(Text) - StartPos;
    FoundAt := FindText(ReplaceDialog1.FindText, StartPos, ToEnd, [stHideMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(ReplaceDialog1.FindText);
    end;
  end;
end;

and for the replace...

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
var
  SelPos: Integer;
begin
  with TReplaceDialog(Sender) do
  begin
    { Perform a global case-sensitive search for FindText in Memo1 }
    SelPos := Pos(FindText, Memo1.Lines.Text);
    if SelPos > 0 then
    begin
      Memo1.SelStart := SelPos - 1;
      Memo1.SelLength := Length(FindText);
      { Replace selected text with ReplaceText }
      Memo1.SelText := ReplaceText;
    end
    else MessageDlg(Concat('Could not find "', FindText, '" in Memo1.'), mtError, [mbOk], 0);
  end;
end;

Later
BoRiS
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350361
Hi hyper66, BoRiS,

BoRiS

FindText(...  is not available in MemoComponent,

Hyper66,

not the best solution follows,

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

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
{ Only for ReplaceButton in ReplaceDialog }
var
  SelPos: Integer;
  SelTmp: Integer;
begin
  if Memo1.sellength > 0 then
    Selpos := Memo1.SelStart + Memo1.SelLength
  else
    SelPos := Memo1.SelStart;
  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;
  if SelPos > 0 then
  begin
    Memo1.SelStart := SelPos - 1;
    Memo1.SelLength := Length(ReplaceDialog1.FindText);
    Memo1.SelText := ReplaceDialog1.ReplaceText;
  end
  else MessageDlg(ReplaceDialog1.FindText + ' not found in Memo1', mtError, [mbOk], 0);
end;

meikl

0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350362
hi hyper66,

this works like find and replace in wordpad
wholeword and raplace all are not implemented
you can search casesensitive or not



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; {Hold CursorPos before enter Dialog}


procedure TForm1.Button1Click(Sender: TObject);
begin
  oldpos := memo1.selstart;
  Replacedialog1.FindText := copy(Memo1.text,memo1.selstart+1,memo1.sellength); {If selected something then take it over}
  ReplaceDialog1.Execute;
end;

Function StrUp (S : String) : String;
{ Use for none CaseSensitivity }
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
  { Check For CaseSens }
  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 nicht finden.', mtError, [mbOk], 0);
  memo1.setfocus; {Force Memo, to show selection}
end;

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

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

end.


Meikl
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1350363
hi hyper66,

replace the
procedure TForm1.ReplaceDialog1Replace(Sender: TObject);

with this (replaces and searchnext automatically!)

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;


meikl
0
 

Author Comment

by:hyper66
ID: 1350364
Thanks guys.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

886 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