Solved

"TRichEdit" and "OnFindText" and "OnReplaceText"

Posted on 1998-06-26
2
294 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
Comment Utility
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
Comment Utility
Thanks to Jacco,you'r too nice :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 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