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

x
?
Solved

"TRichEdit" and "OnFindText" and "OnReplaceText"

Posted on 1998-06-26
2
Medium Priority
?
308 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 200 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

783 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