Solved

"TRichEdit" and "OnFindText" and "OnReplaceText"

Posted on 1998-06-26
2
302 Views
Last Modified: 2010-04-04
I want to find and replace a word in a TRichEdit control.
I used first,TReplaceDialog and "Replace all" button but i do not want a prompt each time i want to do this action.

Please ,send me examples sources.
 
0
Comment
Question by:fromager
2 Comments
 
LVL 10

Accepted Solution

by:
Jacco earned 50 total points
ID: 1355967
The trick is that the property options of the TReplaceDailog are not static. You can change them by selecting "whoile word" and "match case" or by pressing one of the buttons.

You have to determine what the options are before doing the OnFind and OnReplace.

The following code illustrates that:

the unit
*** begin of code ***
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    ReplaceDialog1: TReplaceDialog;
    RichEdit1: TRichEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure ReplaceDialog1Find(Sender: TObject);
    procedure ReplaceDialog1Replace(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var
  SPos : Integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // this shows the dialog
  ReplaceDialog1.Execute;
  // initial position before text
  SPos := -1;
end;

procedure TForm1.ReplaceDialog1Find(Sender: TObject);
var
  ST : TSearchTypes;
  s : String;
begin
  with ReplaceDialog1 do begin
    // copy the search options from the dialog into ST
    if frWholeWord in Options then ST := ST + [stWholeWord];
    if frMatchCase in Options then ST := ST + [stMatchCase];
    // copy the text to find
    s:=FindText;
  end;
  with RichEdit1 do begin
    // find the next match
    SPos:=FindText(s,SPos+1,MaxInt,ST);
    // if found
    if SPos<>-1 then begin
      // select the text
      SelStart:=SPos;
      SelLength := Length(s);
    end else begin
      // tell user that the end is reached
      ShowMessage('end of text reached');
      // set the cursor to the end
      SelStart:=-1;
      // reinitialize the search position
      SPos := -1;
    end;
    // focus the RichEdit
    SetFocus;
  end;
end;

procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
begin
  // check if a 'replace all' or a 'replace' is requested
  if frReplaceAll in ReplaceDialog1.Options then begin
    // replace all
    with RichEdit1 do begin
      repeat
        // execute a find
        ReplaceDialog1Find(Self);
        // if found
        if SPos<>-1 then begin
          // replace the text
          SelText := ReplaceDialog1.ReplaceText;
          // adjust search position
          SPos:=SelStart+SelLength;
        end;
      // end if last found
      until SPos = -1;
    end;
  end else begin
    // replace
    with RichEdit1 do begin
      // check and replace
      if SelText = ReplaceDialog1.FindText then SelText := ReplaceDialog1.ReplaceText;
    end;
  end;
end;

end.
*** end of code ***

the form

*** begin of code ***
object Form1: TForm1
  Left = 200
  Top = 108
  Width = 435
  Height = 300
  Caption = 'Form1'
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  PixelsPerInch = 96
  TextHeight = 13
  object RichEdit1: TRichEdit
    Left = 8
    Top = 8
    Width = 337
    Height = 257
    Lines.Strings = (
      'this is the text in a richedit this is the text in a richeditthis is the text in a '
      'richedit this is the text in a rich edit this is the text in a richedit this is '
      'the text in a richedit hello this is the text in a richedit this is the text in a '
      'richedit this is the text in a richedit this is the text in a richedit this is the '
      'text in a richedit this is the text hello in a richedit this is the text in a '
      'richedit this is the text in a richedit this is the text in a richedit this is the '
      'text in a richedit this is the text in a richedit ')
    TabOrder = 0
  end
  object Button1: TButton
    Left = 352
    Top = 88
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object ReplaceDialog1: TReplaceDialog
    OnFind = ReplaceDialog1Find
    OnReplace = ReplaceDialog1Replace
    Left = 352
    Top = 16
  end
end
*** end of code ***

Good luck with it.

Let hope nobody answered before because I spend a lot of time doing this code sample.

Regards

Jacco
0
 

Author Comment

by:fromager
ID: 1355968
Thanks to Jacco,you'r too nice :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…

830 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