RSS
热门关键字:  ocx  stringgrid  曲线  com  teechart
当前位置 :| 主页>控件开发>

如何在RichEdit中实现向上查找

作者:Admin 时间:2008-09-02 Tag: 点击:

问题说明:在RichEdit中可以用FindText实现向下查找,但我不知如何向上。 试过在调用的时候把EndPos改了比StartPos小,但根本就找不到。 请教!
用户:iseek
ghifi37 我的程序中是用下面的函数实现查找替换的,其中涉及到向上,你可以参考一下: ======================================================================== 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;
用户:beta
我的办法:把源字符串和待查找子串均 Reverse(反序) 然后查找,找到的第一个自然是原顺序的最后一个 至于找到的位置只需作一个简单的减法即可转换回去
用户:iseek
beta, 如果源字符串很大,反序要花很多时间,恐怕比较慢。
用户:ghifi37
各位如果有更简单或是更高效的方案请不要吝惜贴出来!
beta-20,iseek-30,的回答最终被接受。
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
赞助商提供