Memo 是否有類似 RichEdit 的 FindText 功能的做法 ? |
答題得分者是:amen
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
amen
一般會員 發表:4 回覆:8 積分:7 註冊:2006-08-04 發送簡訊給我 |
<textarea class="delphi" rows="10" cols="60" name="code">
var
FoundAt: LongInt;
StartPos, ToEnd: Integer;
begin
with Memo1 do
begin
if SelLength <> 0 then
StartPos := SelStart SelLength
else
StartPos := 0;
ToEnd := Length(Text) - StartPos;
FoundAt := FindText(Edit3.Text, StartPos, ToEnd, [stMatchCase]);
ActiveControl := Memo1;
Memo1.perform(em_setsel, StartPos-255, StartPos 100);
Memo1.Perform(EM_SCROLLCARET, 0, 0);
if FoundAt <> -1 then
begin
SetFocus;
SelStart := FoundAt;
SelLength := Length(Edit3.Text);
end;
if FoundAt = -1 then showmessage('"' Edit3.text '" cannot be found');
end;
end;
</textarea>
|
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
|
amen
一般會員 發表:4 回覆:8 積分:7 註冊:2006-08-04 發送簡訊給我 |
試試看 http://www.delphipages.com/threads/thread.cfm?ID=130376&G=129528
<textarea class="delphi" rows="10" cols="60" name="code">function FindText(const TextToFind, Text: string; StartPos: integer; CaseSensitive, WholeWord: boolean): integer; const WhiteSpaces : set of char = [ '~' , '@' , '#' , '$' , '%' , '^' , '&' , '*' , '(' , ')' , '_' , '-' , ' ' , '=' , '{' , '}' , '[' , ']' , '|' , '\' , ':' , ';' , '"' , '''' , '<' , '>' , ',' , '.' , '?' , '/' , #0, #9, #10, #13, #32]; var target, source, buffer : string; a, b, lentarget, lensrc: integer; begin result := -1; if (TextToFind <> '') and (Text <> '') then begin lentarget := Length(TextToFind); lensrc := Length(Text); SetLength(buffer, lentarget); if lentarget > lensrc then exit; if CaseSensitive then begin target := TextToFind; source := Text; end else begin target := AnsiLowerCase(TextToFind); source := AnsiLowerCase(Text); end; if StartPos < 1 then a := 1 else a := StartPos; while (a <= lensrc) and (result < 0) do begin if (source[a] = target[1]) and (a lentarget -1 <= lensrc) and (source[a lentarget -1] = target[lentarget]) then begin Move(source[a], buffer[1], lentarget); if buffer = target then begin if WholeWord then begin b := a lentarget -1; if ( (a = 1) or (source[a -1] in WhiteSpaces) ) and ( (b = lensrc) or (source[b 1] in WhiteSpaces) ) then result := a; end else result := a; end; end; Inc(a); end; end; end; </textarea><br /> or How to search text in a TMemo -------------------------------------------------------------------------------- I need a search function similar to the one found in TRichEdit, but for the TMemo component. To implement a find next (using a Find dialog on a memo) you need to 1. store the position of the last found word 2. check in the OnFind handler if the frFindNext flag is set 3. if so, modify the Pos statement so it will not search the full text but only the part after the last position. To satisfy 1., we make SelPos a field of TForm1, named FSelPos. So it does no longer appear in the Var section of the OnFind handler. Before you Execute the find dialog, set the FSelPos field to 0. <textarea class="delphi" rows="10" cols="60" name="code"> procedure TForm1.FindDialog1Find(Sender: TObject); var S: String; startpos: Integer; begin with TFindDialog(Sender) do begin {If the stored position is 0 this cannot be a find next. } if FSelPos = 0 then Options := Options - [frFindNext]; {Figure out where to start the search and get the corresponding text from the memo} if frfindNext In Options then begin {This is a find next, start after the end of the last found word} StartPos := FSelPos Length( Findtext ); S := Copy( Memo1.Lines.Text, StartPos, MaxInt ); end else begin {This is a find first, start at the, well, start} S := Memo1.Lines.Text; StartPos := 1; end; {Perform a global case-sensitive search for FindText in S} FSelPos := Pos(FindText, S); if FSelPos > 0 then begin {Found something, correct position for the location of the start of search} FSelPos := FSelPos StartPos - 1; Memo1.SelStart := FSelPos - 1; Memo1.SelLength := Length(FindText); end else begin {No joy, show a message} if frfindNext In Options then S := Concat('There are no further occurences of "', FindText, '" in Memo1.') else S := Concat('Could not find "', FindText, '" in Memo1.'); MessageDlg(S, mtError, [mbOk], 0); end; end; end; </textarea> |
pcboy
版主 發表:177 回覆:1838 積分:1463 註冊:2004-01-13 發送簡訊給我 |
<textarea class="delphi" rows="10" cols="60" name="code"> procedure TForm1.FormCreate(Sender: TObject); var i: Integer; begin i:=FindText('test', Memo1.Text, 0, False, False); Memo1.Lines.Add(IntToStr(i)); // 成功, 它的傳回值是字串最後一個字母所在 FindDialog1.FindText:='test'; //FindDialog1Find; 錯誤, 這要怎麼呼叫 ? end;</textarea>
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案! 子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問! |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |