DELPHI 如何在Richedit 中替换关键字  
官方Delphi 学习QQ群: 682628230(三千人)
频道

DELPHI 如何在Richedit 中替换关键字


procedure TForm1.ReplaceDialog1Replace(Sender: TObject);

 

var

  SelPos: Integer;

begin

  with TReplaceDialog(Sender) do

  begin

    SelPos := Pos(FindText, RichEdit1.Lines.Text);

    if SelPos > 0 then

    begin

      RichEdit1.SelStart := SelPos - 1;

      RichEdit1.SelLength := Length(FindText);

      { Replace selected text with ReplaceText }

      RichEdit1.SelText := ReplaceText;

    end

    else MessageDlg(Concat('No more matches for: "', FindText, '" have been found in the current document'), mtError, [mbOk], 0);

 

  end;

 

end;



现在,插入新按钮并为其添加OnClick事件(与之前添加的方式相同),然后粘贴以下代码:


ReplaceDialog1.Execute;



And finally, add OnFind event to the ReplaceDialog


Delphi

procedure TForm1.ReplaceDialogFind(Sender: TObject);

var

  FoundAt: LongInt;

  StartPos, ToEnd: Integer;

begin

  with RichEdit1 do

  begin

{ if there's selected text, start looking from that place, in other case, start looking from the beginning }

    if SelLength <> 0 then

      StartPos := SelStart + SelLength

    else

      StartPos := 0;

 

  { find the end of text }

    ToEnd := Length(Text) - StartPos;

 

    FoundAt := FindText(ReplaceDialog1.FindText, StartPos, ToEnd, [stMatchCase]);

    if FoundAt <> -1 then

    begin

      SetFocus;

      SelStart := FoundAt;

      SelLength := Length(ReplaceDialog1.FindText);

    end;

  end;

end;



推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.19168496131897 seconds