請問如何在DBMemo OR DBRichEdit 做到搜尋功能? |
尚未結案
|
rookie
一般會員 ![]() ![]() 發表:26 回覆:38 積分:12 註冊:2003-04-23 發送簡訊給我 |
|
banson1716
高階會員 ![]() ![]() ![]() ![]() 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
試試看 public
function find(tofind,replacewith: string): string;
{ Public declarations }
end; var
Form1: TForm1;
replall: boolean; implementation {$R *.DFM} function TForm1.find(tofind,replacewith: string): string;
var
matchcase,wholeword,up,top: boolean;
FindIn,findin2,uptofind,upfindin,upfindin2: string;
find1s,find2s,find3s,find4s: string;
find1,find2,find3,find4: integer;
flen,start,holder,found,index,i: integer;
begin
if top = true then
begin
richedit1.perform(em_setsel,0,0);
top := false;
end;
if matchcase = true then
begin
if lowercase(richedit1.seltext) = lowercase(tofind) then
if replacewith <> '' then richedit1.seltext:= replacewith;
findin := richedit1.text;
end else begin
ToFind := lowercase(ComboBox1.text);
if replacewith <> '' then replacewith := lowercase(replacewith);
if lowercase(richedit1.seltext) = lowercase(tofind) then
if replacewith <> '' then richedit1.seltext := replacewith;
findin := lowercase(richedit1.text);
end;
flen := length(tofind);
found := 0;
index := -1;
if up = true then begin
start := richedit1.selstart;
holder := start +1;
for i := 0 to
length(tofind)-1 do appendstr(uptofind, copy(tofind,length(tofind)-i,1));
while found = 0 do begin
if holder < -250 then begin
showmessage('"'+tofind+'" cannot be found');
replall := false; {replall = global boolean var}
activecontrol := richedit1;
exit;
end;
if holder < 250 then i := holder -1
else
begin
i := 250;
end;
findin2 := concat(' ',copy(findin,holder-i,i),' ');{grab 250 chunks to search}
upfindin2 := '';
for i := 0 to length(findin2)-1 do appendstr(upfindin2, copy(findin2,length(findin2)-i,1));
if wholeword = true then begin
find1s := ' ' + uptofind + ' ';
find2s := #13 + uptofind + ' ';
find3s := #13 + uptofind + #10;
find4s := ' ' + uptofind + #10;
find1 := pos(find1s, upfindin2);
find2 := pos(find2s, upfindin2);
find3 := pos(find3s, upfindin2);
find4 := pos(find4s, upfindin2);
if find1 = 0 then find1 := 300; {change notfound for next test}
if find2 = 0 then find2 := 300;
if find3 = 0 then find3 := 300;
if find4 = 0 then find4 := 300;
{find the lowest found value (first found) and set 'found' to it}
if (find1 <300) and (find1
|
rookie
一般會員 ![]() ![]() 發表:26 回覆:38 積分:12 註冊:2003-04-23 發送簡訊給我 |
感謝banson1716前輩的回答
由於我只要用簡單的搜尋功能
於是我把程式簡化如下
procedure TForm2.Button10Click(Sender: TObject); var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
//
with dbRichEdit1 do
begin
if SelLength <> 0 then
StartPos := SelStart + SelLength
else
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(edit8.Text, StartPos, ToEnd, [stMatchCase]);
ActiveControl := dbrichedit1;
dbrichedit1.perform(em_setsel, StartPos-255, StartPos+100);
dbRichEdit1.Perform(EM_SCROLLCARET, 0, 0);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(edit8.Text);
end;
if FoundAt = -1 then showmessage('"'+edit8.text+'" cannot be found');
end;
end; < >< >
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |