程序中的对话框应用(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;
编程语言无所谓高低级,唯适用罢了。语言结构再天花乱坠,富丽堂皇终不过CPU上的0和1。