Solved

"TRichEdit" and "OnFindText" and "OnReplaceText"

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

22 Experts available now in Live!

Get 1:1 Help Now