TFindDialog组件用于显示一个查找对话框,允许用户在文件中查找文本。
1、设置“查找”对话框显示时的位置,通常打开查找对话框时,出现的位置可能会影响视觉效果,下面示例可以解决。
procedure TForm1.Button2Click(Sender: TObject); var Points: TPoint; begin Points.x:= 200; Points.Y:= 400;//此例只设置位置,并未实现查找功能 FindDialog1.FindText:= Edit1.Text; FindDialog1.Position:= Point(RichEdit1.left + RichEdit1.width,RichEdit1.top); FindDialog1.Position:= points; FindDialog1.Execute; end;
2、实现查找功能示例,窗体上添加按钮,Edit,RichEdit,FindDialog组件。
procedure TForm1.Button2Click(Sender: TObject); begin if FindDialog1.Execute then begin FindDialog1.top:= 200; FindDialog1.Left:= 400; FindDialog1.Execute; end; end; procedure TForm1.RichEdit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); begin if Shift= [ssCtrl] then //按下<Ctrl+F>键时也会打开查找对话框 if key = 70 then Button2.Click; end; procedure TForm1.FindDialog1Find(Sender: TObject); var foundAt: LongInt; startPos,endPos: Integer; begin with RichEdit1 do begin if SelLength <> 0 then startPos:= SelStart + SelLength else startPos:= 0; endPos:= Length(Text)- startPos; foundAt:= FindText(FindDialog1.FindText,startPos,endPos,[stMatchCase]); if foundAt<> -1 then begin SetFocus; SelStart:= foundAt; SelLength:= Length(FindDialog1.FindText); end; end; end;