Solved

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

Posted on 1998-12-10
8
787 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
[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
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Accepted Solution

by:
BoRiS earned 150 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…
Suggested Courses
Course of the Month5 days, 19 hours left to enroll

626 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