现在要使用RichEdit或者Memo控件实现字符串搜索.效果如同使用写字板或者记事本搜索字符串.:
使用一个edit.text中的字符作为关键字.按下一个按钮后.richedit或者memo控件便可以高亮显示出这个关键字.
相当于写一个写字板或者记事本的搜索功能.我试了很长时间.一直找不到思路.还请大家帮忙.
感谢.
使用一个edit.text中的字符作为关键字.按下一个按钮后.richedit或者memo控件便可以高亮显示出这个关键字.
相当于写一个写字板或者记事本的搜索功能.我试了很长时间.一直找不到思路.还请大家帮忙.
感谢.
type
TSearchType = (stWholeWord, stMatchCase);
TSearchTypes = set of TSearchType;
function FindText(const SearchStr: string; StartPos, Length: Integer; Options: TSearchTypes): Integer;下面是Delphi自带的例子:This example requires a TRichEdit, a TButton, and a TFindDialog.
Clicking the button click will display a Find Dialog to the right of the edit control. Filling in the "Find what" text and pressing the Find Next button will select the first matching string in the Rich Edit control that follows the previous selection.procedure TForm1.Button1Click(Sender: TObject);begin
FindDialog1.Position := Point(RichEdit1.Left + RichEdit1.Width, RichEdit1.Top);
FindDialog1.Execute;
end;procedure TForm1.FindDialog1Find(Sender: TObject);
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with RichEdit1 do
begin
{ begin the search after the current selection if there is one }
{ otherwise, begin at the start of the text }
if SelLength <> 0 then StartPos := SelStart + SelLength
else StartPos := 0; { ToEnd is the length from StartPos to the end of the text in the rich edit control } ToEnd := Length(Text) - StartPos; FoundAt := FindText(FindDialog1.FindText, StartPos, ToEnd, [stMatchCase]);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(FindDialog1.FindText);
end;
end;
end;
if edit3.Text<>'' then
begin
Foundbj:=datafrm.richedit1.FindText(edit3.text,0,length(datafrm.richedit1.Text),[stMatchCase]);
datafrm.richedit1.SelStart := Foundbj;
datafrm.richedit1.SelLength := ((Length('edit3.text')-1) div 2);
// datafrm.richedit1.SelAttributes.Color:=rgb(255,0,0);
// datafrm.RichEdit1.SelLength:=0;
end;
楼主可以以此例子作为基础,当然必须扩充一些功能,但毕竟基本的架子已经出来了,你也不需要找什么思路了。