Solved

"TRichEdit" and "OnFindText" and "OnReplaceText"

Posted on 1998-06-26
2
300 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now