elbaid
asked on
How to perform a Search with a TMemo?
I would like to know how to perform a search in a tmemo component with a Tfinddialog. You know, exactly like when you choose the "Find..." function in a word processor or in a text editor. I've got this code but it doesn't works properly, i think something is missing in the code:
procedure TForm1.Find1Click(Sender: TObject);
begin
I := 0;
IF FindDialog1.Execute then
begin
while (Pos(FindDialog1.FindText, Memo1.Lines[I]) = 0)
AND (I <= (Memo1.Lines.Count+1)) do Inc(I);
end;
end;
If you need some specifications to this question just ask me.
procedure TForm1.Find1Click(Sender: TObject);
begin
I := 0;
IF FindDialog1.Execute then
begin
while (Pos(FindDialog1.FindText,
AND (I <= (Memo1.Lines.Count+1)) do Inc(I);
end;
end;
If you need some specifications to this question just ask me.
Hi,
this code works for case sensitive search and it can not detect wrapping words. The casre insensitive version can be as follows:
procedure TForm1.Find1Click(Sender: TObject);
begin
I := 0;
IF FindDialog1.Execute then
begin
while (Pos(LowerCase(FindDialog1 .FindText) , LowerCase(Memo1.Lines[I])) = 0)
AND (I <= (Memo1.Lines.Count+1)) do Inc(I);
end;
end;
igor
this code works for case sensitive search and it can not detect wrapping words. The casre insensitive version can be as follows:
procedure TForm1.Find1Click(Sender: TObject);
begin
I := 0;
IF FindDialog1.Execute then
begin
while (Pos(LowerCase(FindDialog1
AND (I <= (Memo1.Lines.Count+1)) do Inc(I);
end;
end;
igor
More code from Delphi help is as follows:
(associate this with the find dialog find event and remove the previous code)
procedure TForm1.FindDialog1Find(Sen der: TObject);
var
I, J, PosReturn, SkipChars: Integer;
begin
for I := 0 to Memo1.Lines.Count do
begin
// I added case insensitive search here
PosReturn := Pos(LowerCase(FindDialog1. FindText), LowerCase( Memo1.Line s[I]));
if PosReturn <> 0 then {found!}
begin
Skipchars := 0;
for J := 0 to I - 1 do
Skipchars := Skipchars + Length(Memo1.Lines[J]);
SkipChars := SkipChars + (I*2);
SkipChars := SkipChars + PosReturn - 1;
Memo1.SetFocus;
Memo1.SelStart := SkipChars;
Memo1.SelLength := Length(FindDialog1.FindTex t);
Break;
end;
end;
end;
(associate this with the find dialog find event and remove the previous code)
procedure TForm1.FindDialog1Find(Sen
var
I, J, PosReturn, SkipChars: Integer;
begin
for I := 0 to Memo1.Lines.Count do
begin
// I added case insensitive search here
PosReturn := Pos(LowerCase(FindDialog1.
if PosReturn <> 0 then {found!}
begin
Skipchars := 0;
for J := 0 to I - 1 do
Skipchars := Skipchars + Length(Memo1.Lines[J]);
SkipChars := SkipChars + (I*2);
SkipChars := SkipChars + PosReturn - 1;
Memo1.SetFocus;
Memo1.SelStart := SkipChars;
Memo1.SelLength := Length(FindDialog1.FindTex
Break;
end;
end;
end;
you could also do something like this:
it does not use TFindDialog..Just a simple procedure....
procedure GoTo(Memo : TMemo; sWord : string);
var
nPos : Integer;
begin
nPos := Pos(sWord, Memo.Text);
if nPos > 0 then begin
Memo.SetFocus;
Memo.SelStart := nPos;
Memo.SelLength(Length(sWor d));
end;
end;
Example call: GoTo(Memo1, 'Anything');
Note: Haven't tested this so you might need a bit of modification...
Regards,
Viktor Ivanov
it does not use TFindDialog..Just a simple procedure....
procedure GoTo(Memo : TMemo; sWord : string);
var
nPos : Integer;
begin
nPos := Pos(sWord, Memo.Text);
if nPos > 0 then begin
Memo.SetFocus;
Memo.SelStart := nPos;
Memo.SelLength(Length(sWor
end;
end;
Example call: GoTo(Memo1, 'Anything');
Note: Haven't tested this so you might need a bit of modification...
Regards,
Viktor Ivanov
hi, elbaid.
try this:
procedure TForm1.FindDialog1Find(Sen der: TObject);
var Buff, P, FT : PChar;
BuffLen : Word;
begin
with Sender as TFindDialog do begin
GetMem(FT, Length(FindText) + 1);
StrPCopy(FT, FindText);
BuffLen := Memo1.GetTextLen + 1;
GetMem(Buff, BuffLen);
Memo1.GetTextBuf(Buff, BuffLen);
if Memo1.SelStart <> 0 then
P:= Buff + Memo1.SelStart + Memo1.SelLength
else
P := Buff;
P:= StrPos(P, FT);
if P = NIL then MessageBeep(0)
else
begin
Memo1.SelStart:= P - Buff;
Memo1.SelLength:= Length(FindText);
end;
FreeMem(FT, Length(FindText) + 1);
FreeMem(Buff,BuffLen);
Memo1.SetFocus;
end;
end;
this ain't case insensitive. to do this, you gotta change it a little bit.
if you want to find wrapping words, you will have to replace each #13#10 with nothing i.e. an empty string. if you need a StrReplace func, tell us.
you will also have to implement the "searching upwards" by yourself.
anyway, this is quite fast.
i'll see if i find some time tonight to improve it.
so long,
Black Death.
try this:
procedure TForm1.FindDialog1Find(Sen
var Buff, P, FT : PChar;
BuffLen : Word;
begin
with Sender as TFindDialog do begin
GetMem(FT, Length(FindText) + 1);
StrPCopy(FT, FindText);
BuffLen := Memo1.GetTextLen + 1;
GetMem(Buff, BuffLen);
Memo1.GetTextBuf(Buff, BuffLen);
if Memo1.SelStart <> 0 then
P:= Buff + Memo1.SelStart + Memo1.SelLength
else
P := Buff;
P:= StrPos(P, FT);
if P = NIL then MessageBeep(0)
else
begin
Memo1.SelStart:= P - Buff;
Memo1.SelLength:= Length(FindText);
end;
FreeMem(FT, Length(FindText) + 1);
FreeMem(Buff,BuffLen);
Memo1.SetFocus;
end;
end;
this ain't case insensitive. to do this, you gotta change it a little bit.
if you want to find wrapping words, you will have to replace each #13#10 with nothing i.e. an empty string. if you need a StrReplace func, tell us.
you will also have to implement the "searching upwards" by yourself.
anyway, this is quite fast.
i'll see if i find some time tonight to improve it.
so long,
Black Death.
hi, elbaid.
try this:
procedure TForm1.FindDialog1Find(Sen der: TObject);
var Buff, P, FT : PChar;
BuffLen : Word;
begin
with Sender as TFindDialog do begin
GetMem(FT, Length(FindText) + 1);
StrPCopy(FT, FindText);
BuffLen := Memo1.GetTextLen + 1;
GetMem(Buff, BuffLen);
Memo1.GetTextBuf(Buff, BuffLen);
if Memo1.SelStart <> 0 then
P:= Buff + Memo1.SelStart + Memo1.SelLength
else
P := Buff;
P:= StrPos(P, FT);
if P = NIL then MessageBeep(0)
else
begin
Memo1.SelStart:= P - Buff;
Memo1.SelLength:= Length(FindText);
end;
FreeMem(FT, Length(FindText) + 1);
FreeMem(Buff,BuffLen);
Memo1.SetFocus;
end;
end;
this ain't case insensitive. to do this, you gotta change it a little bit.
if you want to find wrapping words, you will have to replace each #13#10 with nothing i.e. an empty string. if you need a StrReplace func, tell us.
you will also have to implement the "searching upwards" by yourself.
anyway, this is quite fast.
i'll see if i find some time tonight to improve it.
so long,
Black Death.
try this:
procedure TForm1.FindDialog1Find(Sen
var Buff, P, FT : PChar;
BuffLen : Word;
begin
with Sender as TFindDialog do begin
GetMem(FT, Length(FindText) + 1);
StrPCopy(FT, FindText);
BuffLen := Memo1.GetTextLen + 1;
GetMem(Buff, BuffLen);
Memo1.GetTextBuf(Buff, BuffLen);
if Memo1.SelStart <> 0 then
P:= Buff + Memo1.SelStart + Memo1.SelLength
else
P := Buff;
P:= StrPos(P, FT);
if P = NIL then MessageBeep(0)
else
begin
Memo1.SelStart:= P - Buff;
Memo1.SelLength:= Length(FindText);
end;
FreeMem(FT, Length(FindText) + 1);
FreeMem(Buff,BuffLen);
Memo1.SetFocus;
end;
end;
this ain't case insensitive. to do this, you gotta change it a little bit.
if you want to find wrapping words, you will have to replace each #13#10 with nothing i.e. an empty string. if you need a StrReplace func, tell us.
you will also have to implement the "searching upwards" by yourself.
anyway, this is quite fast.
i'll see if i find some time tonight to improve it.
so long,
Black Death.
ASKER
My answer was a mix with inter's second answer and Black Death's answer, so thanks guys. Is it possible to give some points to Black Death?
Elbaid
Elbaid
i think you could ask linda to let her split the points between us as you desire.
have a nice day,
:-)
Black Death.
have a nice day,
:-)
Black Death.
btw:
if you look at delphi 1 - demo "textdemo", there's a unit "search.pas" which implements just what you want the borland style. i found this last night.
if you ain't got delphi 1, post your email address here and i'll mail you the sources.
so long,
Black Death.
if you look at delphi 1 - demo "textdemo", there's a unit "search.pas" which implements just what you want the borland style. i found this last night.
if you ain't got delphi 1, post your email address here and i'll mail you the sources.
so long,
Black Death.
ASKER
k cool, you can send this to elbaid@hotmail.com
thanks!
elbaid
thanks!
elbaid
elbaid sent me the following email:
I want to split my 150 points given to question Q.10085824 to inter and
Black Death. 50 to inter and 100 to Black Death because the question
isn't actualy answered. They send their answers by comments.
So this question can be saved, I'd like BlackDeath to post an answer to the question. The question can then be graded and saved in the Previously Asked Questions (PAQ).
I'll post another question in this topic area for inter to award him/her points for the contribution to this question.
Linda Gardner
Customer Service @ Experts Exchange.
I want to split my 150 points given to question Q.10085824 to inter and
Black Death. 50 to inter and 100 to Black Death because the question
isn't actualy answered. They send their answers by comments.
So this question can be saved, I'd like BlackDeath to post an answer to the question. The question can then be graded and saved in the Previously Asked Questions (PAQ).
I'll post another question in this topic area for inter to award him/her points for the contribution to this question.
Linda Gardner
Customer Service @ Experts Exchange.
ok, linda-darling. here we go.
thanx to elbaid.
greetings to inter. is this ok for you?
have a nice day - all of you,
Black Death.
thanx to elbaid.
greetings to inter. is this ok for you?
have a nice day - all of you,
Black Death.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER