用这个可以向下查找,怎么向上呢?function TssnRichEditor.FindNext(Text: String;
Option: TFindOptions): Boolean;
var
FoundAt : LongInt;
StartPos, ToEnd : Integer;
begin
Result := false; StartPos := m_Edit.SelStart + m_Edit.SelLength;
ToEnd := Length(m_Edit.Text) - StartPos;
if (frMatchCase in Option) then
FoundAt := m_Edit.FindText(Text, StartPos, ToEnd, [stMatchCase])
else
FoundAt := m_Edit.FindText(Text, StartPos, ToEnd, []); if FoundAt = -1 then
Exit; m_Edit.SetFocus();
m_Edit.SelStart := FoundAt;
m_Edit.SelLength := Length(WideString(Text));
Result := true;
end;
Option: TFindOptions): Boolean;
var
FoundAt : LongInt;
StartPos, ToEnd : Integer;
begin
Result := false; StartPos := m_Edit.SelStart + m_Edit.SelLength;
ToEnd := Length(m_Edit.Text) - StartPos;
if (frMatchCase in Option) then
FoundAt := m_Edit.FindText(Text, StartPos, ToEnd, [stMatchCase])
else
FoundAt := m_Edit.FindText(Text, StartPos, ToEnd, []); if FoundAt = -1 then
Exit; m_Edit.SetFocus();
m_Edit.SelStart := FoundAt;
m_Edit.SelLength := Length(WideString(Text));
Result := true;
end;
怎么向前推进阿?
procedure TfrmFindReplace.bitFindClick(Sender: TObject);
var
Text : String; //欲查找的字符串
Option : TFindOptions; //查找选项
FoundAt : Integer; //在何处查到的(字符串索引)
LastFoundAt : Integer; //最后一个
AllText : String; //要查找的文本
begin
Option:=[];
if chkUp.Checked=false then Option:=Option+[frDown];
if chkMatchCase.Checked=true then Option:=Option+[frMatchCase];
Text:=edtFindTxtC.Text;
//决定查找的选项 if frDown in Option then
AllText:=System.Copy(
frmMain.dbmCode.Text,
frmMain.dbmCode.SelStart+frmMain.dbmCode.SelLength+1,
Length(frmMain.dbmCode.Text)
)
else
AllText:=System.Copy(frmMain.dbmCode.Text,1,frmMain.dbmCode.SelStart);
if not (frMatchCase in Option) then
begin
AllText:=UpperCase(AllText);
Text:=UpperCase(Text);
end; if frDown in Option then
begin
FoundAt:=Pos(Text,AllText);
if FoundAt=0 then
begin
Application.MessageBox('没有找到代码语句!'+Chr(13)+Chr(13)+'- 请单击[确定]按钮。',
'SBS - 没有找到',
MB_OK+MB_ICONEXCLAMATION);
Exit;
end;
frmMain.dbmCode.SelStart:=frmMain.dbmCode.SelStart+frmMain.dbmCode.SelLength+FoundAt;
end
else begin
LastFoundAt:=0;
repeat
FoundAt:=Pos(Text,AllText);
if FoundAt<>0 then
begin
AllText:=System.Copy(
AllText,
FoundAt+1,
Length(AllText)
);
LastFoundAt:=LastFoundAt+FoundAt;
end
until FoundAt=0;
if LastFoundAt=0 then
begin
Application.MessageBox('没有找到代码语句!'+Chr(13)+Chr(13)+'- 请单击[确定]按钮。',
'SBS - 没有找到',
MB_OK+MB_ICONEXCLAMATION);
Exit;
end;
frmMain.dbmCode.SelStart:=LastFoundAt;
end;
frmMain.dbmCode.SelLength:=Length(Text);
if frmMain.dbmCode.CanFocus() then frmMain.dbmCode.SetFocus;
{
Application.MessageBox('没有找到代码语句!'+Chr(13)+Chr(13)+'- 请单击[确定]按钮。',
'SBS - 没有找到',
MB_OK+MB_ICONEXCLAMATION);}
end;//修正了一些原书的错误!
//Nicrosoft愤怒道:“抄袭我的《Delphi高手突破》!”可是为了大家,假如公开一下的
//话,我也不用等几个月了呀不是吗?(比尔·盖茨:“侵权!M(N)icosoft”*^_^*)
//最后:接分
========================================================================
function TMainFm.find(tofind, replacewith: string;editor:TrEditor): string;
var
matchcase, wholeword, up, top: boolean;
FindIn, findin2, uptofind, upfindin2: string;
find1s, find2s, find3s, find4s: string;
find1, find2, find3, find4: integer;
flen, start, holder, found, index, i: integer;
begin
//查找参数检查
if CheckBox4.checked then matchcase := true;
if CheckBox3.checked then wholeword := true;
if CheckBox1.checked then up := true;
if CheckBox2.checked then top := true; if top = true then //------------如果是向下------------------
begin
Editor.perform(em_setsel, 0, 0);
CheckBox2.checked := false;
top := false;
end; if matchcase = true then //----------如果区分大小写-----------
begin
ToFind := findEdit.text;
if lowercase(Editor.seltext) = lowercase(tofind) then
if replacewith <> '' then Editor.seltext := replacewith;
findin := Editor.text;
end
else //----------如果不区分大小写-----------
begin
ToFind := lowercase(findEdit.text);
if replacewith <> '' then replacewith := lowercase(replacewith);
if lowercase(Editor.seltext) = lowercase(tofind) then
if replacewith <> '' then Editor.seltext := replacewith;
findin := lowercase(Editor.text);
end;
flen := length(tofind);
found := 0;
index := -1;
if up = true then//-------------如果是向上----------------------
begin
start := Editor.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
if pos(tofind,editor.Text)>0 then
Application.MessageBox('查找结束。', '提示', MB_OK) else
Application.MessageBox(PChar('找不到“'+ tofind + '”'), '提示', MB_OK);
replall := false; //replall = 全局布尔变量
activecontrol := Editor;
exit;
end;
if holder < 250 then
i := holder - 1
else
begin
i := 250;
end;
{add spaces to ends of searchin string for wholeword search}
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); {find each type of wholeword}
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 < find2) and (find1 < find3) and (find1 < find4)
then found := find1 + 1;
if (find2 < 300) and (find2 < find1) and (find2 < find3) and (find2 < find4)
then found := find2 + 1;
if (find3 < 300) and (find3 < find1) and (find3 < find2) and (find3 < find4)
then found := find3 + 1;
if (find4 < 300) and (find4 < find1) and (find4 < find2) and (find4 < find3)
then found := find4 + 1;
end
else
found := pos(uptofind, upfindin2); {not wholeword search}
holder := holder - 225; {225 to allow for possible truncation}
index := index + 1;
end;
if index > 0 then
index := index * 225
else
index := 0;
holder := start - index - found + 3;
ActiveControl := Editor;
Editor.perform(em_setsel, holder - flen, holder);
Editor.perform(em_scrollcaret, 0, 0);
end
else //---------向下找----------
begin
start := Editor.selstart;
if lowercase(Editor.seltext) = lowercase(tofind) then
Editor.perform(em_setsel, start + flen, start + flen);
start := Editor.selstart;
holder := start + 1;
while found = 0 do
begin
if holder > (Editor.gettextlen) + 223 then
begin
if pos(tofind,editor.Text)>0 then
Application.MessageBox('查找结束。', '提示', MB_OK) else
Application.MessageBox(PChar('找不到“'+ tofind + '”'), '提示', MB_OK);
replall := false;
ActiveControl := Editor;
exit;
end;
findin2 := concat(' ', copy(findin, holder, 250), ' ');
if wholeword = true then
begin
find1s := ' ' + tofind + ' ';
find2s := #10 + tofind + ' ';
find3s := #10 + tofind + #13;
find4s := ' ' + tofind + #13;
find1 := pos(find1s, findin2);
find2 := pos(find2s, findin2);
find3 := pos(find3s, findin2);
find4 := pos(find4s, findin2);
if find1 = 0 then find1 := 300;
if find2 = 0 then find2 := 300;
if find3 = 0 then find3 := 300;
if find4 = 0 then find4 := 300;
if (find1 < 300) and (find1 < find2) and (find1 < find3) and (find1 < find4)
then found := find1 + 1;
if (find2 < 300) and (find2 < find1) and (find2 < find3) and (find2 < find4)
then found := find2 + 1;
if (find3 < 300) and (find3 < find1) and (find3 < find2) and (find3 < find4)
then found := find3 + 1;
if (find4 < 300) and (find4 < find1) and (find4 < find2) and (find4 < find3)
then found := find4 + 1;
end
else
found := pos(tofind, findin2);
holder := holder + 225;
index := index + 1;
end;
if index > 0 then
index := index * 225
else
index := 0;
holder := start + index + found - 3;
ActiveControl := Editor;
Editor.perform(em_setsel, holder, holder + flen);
Editor.perform(em_scrollcaret, 0, 0);
end;
end;
我下午和我们老大搞到快吃饭时才终于搞定。
提个醒,clx的memo行为有点怪的:(