zoukankan      html  css  js  c++  java
  • 程序中的对话框应用(6)- “替换”对话框

    TReplaceDialog组件是TFindDialog类的特殊版本,提示用户查找和替换字符串,与“查找”对话框相同,替换对话框是一个无模式的windows对话框。

    代码示例(窗体上添加按钮、RichEdit、ReplaceDialog):

    procedure TForm1.Button1Click(Sender: TObject);//打开替换对话框
    begin
      ReplaceDialog1.Top:= 200;
      ReplaceDialog1.Left:= 400;
      ReplaceDialog1.Execute;
    end;
    
    procedure TForm1.ReplaceDialog1Find(Sender: TObject);//在文本中查找要替换的内容
    var
      FoundAt: LongInt;
      StartPos,Toend: integer;
    begin
      with RichEdit1 do
      begin
        if SelLength<> 0 then
        StartPos:= SelStart+ SelLength
        else
          StartPos:= 0;
        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;
    
    procedure TForm1.ReplaceDialog1Replace(Sender: TObject);
    var
      SelPos: Integer;
    begin
      //对文本进行单个替换
      if frReplace in ReplaceDialog1.Options then
      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);
            RichEdit1.SelText:= ReplaceText;
          end
          else
            MessageDlg(Concat('Could not find',Findtext,'in Memo1.'),mtError,[mbOK],0);
        end;
        //对文本进行全部替换
        if frReplaceAll in ReplaceDialog1.Options then
        while True do
        begin
          with TReplaceDialog(Sender) do
          begin
            if SelPos= 0 then
            Break;
          SelPos:= Pos(FindText,RichEdit1.Lines.Text);
            if SelPos> 0 then
            begin
              RichEdit1.SelStart:= SelPos- 1;
              RichEdit1.SelLength:= Length(FindText);
              RichEdit1.SelText:= ReplaceText;
            end;
          end;
        end;
      end;
    end;

     

  • 相关阅读:
    JavaScript操作符instanceof揭秘
    Linux打开txt文件乱码的解决方法
    Working copy locked run svn cleanup not work
    poj 2299 UltraQuickSort 归并排序求解逆序对
    poj 2312 Battle City 优先队列+bfs 或 记忆化广搜
    poj2352 stars 树状数组
    poj 2286 The Rotation Game 迭代加深
    hdu 1800 Flying to the Mars
    poj 3038 Children of the Candy Corn bfs dfs
    hdu 1983 Kaitou Kid The Phantom Thief (2) DFS + BFS
  • 原文地址:https://www.cnblogs.com/fansizhe/p/12784190.html
Copyright © 2011-2022 走看看