當前位置:編程學習大全網 - 源碼下載 - Delphi中FindDialog的使用問題

Delphi中FindDialog的使用問題

1、思想:查找子串(Pos函數),如果找到就在剩下的子串(Copy函數)中繼續查找,直到結束 i:=Memo1.SelStart+memo1.SelLength ; (記錄選中子串結束的位置,如果未選中,i就是光標所在位置) str :=Copy(Memo1.Text,i+1,Length(Memo1.Text)); (取選中子串後面的部分作為新的字符串,用以查找) PosReturn :=Pos(dlgFindMemo1.FindText,str); (返回第壹次查找的位置,沒有找到就返回0) if PosReturn <>0 then (找到了就把它高亮顯示出來) begin Memo1.SetFocus; (設置Memo1為當前焦點) Memo1.SelStart:=PosReturn+i-1; (所選中子串開始的位置) Memo1.SelLength:=Length(dlgFindMemo1.FindText); (所選中子串的長度,就是查找的長度) end; 2、當點擊取消之後,才會高亮顯示 可能是沒有Memo1.SetFocus這句吧,妳試試,刪掉這句後會出現妳據說的情況 3、我自己參照寫的(向上查找) function TForm1.LastPos(const substr, str: string): Integer; (自定義函數,查找子串最後出現的位置) var ipos: Integer; strtmp: WideString; begin Result := 0; strtmp := str; ipos := Pos(substr,strtmp); while ipos <> 0 do begin Delete(strtmp,1,ipos+Length(substr)-1); Result := Result + ipos; ipos := Pos(substr,strtmp); if ipos = 0 then Break; Result := Result+Length(substr)-1; end; end; //向上查找 var i,PosReturn: Integer; findstr: string; begin i := Memo1.SelStart; findstr := Copy(Memo1.Text,1,i); PosReturn := LastPos(FindDialog1.FindText,findstr); if PosReturn <> 0 then begin Memo1.SetFocus; Memo1.SelStart := PosReturn - 1; Memo1.SelLength := Length(FindDialog1.FindText); end else MessageBox(Self.Handle,Pchar('未能找到'+FindDialog1.FindText+'!'),'提示',MB_OK+MB_ICONINFORMATION); end;

  • 上一篇:死亡交叉介紹及其與技術指標結合運用
  • 下一篇:Linkedlist1.8源代碼
  • copyright 2024編程學習大全網