declaring procedures

Hi,
I have a piece of code for searching text in a RichEdit which works fine with a Button1Click, but stops working when I try to put it in my own procedure:

procedure mysearch(Sender: TObject);
 var
  FoundAt: LongInt;
  StartPos, ToEnd: integer;
begin
  with RichEdit1 do
  begin
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }

    if SelLength <> 0 then
      StartPos := SelStart + SelLength
    else

      StartPos := 0;

    { ToEnd is the length from StartPos to the end of the text in the rich edit control }

    ToEnd := Length(Text) - StartPos;

    FoundAt := FindText(Edit1.Text, StartPos, ToEnd, [stMatchCase]);
    if FoundAt <> -1 then
    begin
      SetFocus;
      SelStart := FoundAt;
      SelLength := Length(Edit1.Text);
    end;
  end;

end;

procedure Form1.Button2Click(Sender: TObject);
begin
mysearch;
end;

Errors: Undeclared identifier RichEdit1
Undeclared identifier: 'SelLength'
Undeclared identifier: 'SelStart'
Undeclared identifier: 'FindText'
among others.

What should I do??

Thanks!

s.
syllviaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mocartsCommented:
procedure MySearch must be procedure of form which contains Richedit1 to access it in code:

TForm1 = class(TForm)
  RichEdit1: TRichEdit;
  Button2: TButton;
  ...
  procedure Button2Click(Sender: TObject);
private
  // you don't need Sender parameter for simple proc.
  procedure MySearch;
end;

implementation
..

procedure TForm1.MySearch;
begin
  with RichEdit1 do begin
..
  end;
end;

wbr, mo.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mocartsCommented:
but.. :)
you can use simple unit function by passing to it RichEdit as parameter and search text:

procedure mysearch(RE: TRichEdit; const FindWhat: string);
var
 FoundAt: LongInt;
 StartPos, ToEnd: integer;
begin
 with RE do
 begin
   { begin the search after the current selection if there is one }
   { otherwise, begin at the start of the text }

   if SelLength <> 0 then
     StartPos := SelStart + SelLength
   else

     StartPos := 0;

   { ToEnd is the length from StartPos to the end of the text in the rich edit control }

   ToEnd := Length(Text) - StartPos;
  // substitute Edit1.Text with FindWhat parameter
   FoundAt := FindText(FindWhat, StartPos, ToEnd, [stMatchCase]);
   if FoundAt <> -1 then
   begin
     SetFocus;
     SelStart := FoundAt;
     SelLength := Length(FindWhat);
   end;
 end;

end;

and call from buttonclick:
procedure TForm1.Button1Click(Sender: TObject);
begin
  MySearch(Richedit1, FindWhat);
end;

wbr, mo.
0
mocartsCommented:
sorr, must be:

procedure TForm1.Button1Click(Sender: TObject);
begin
  MySearch(Richedit1, Edit1.Text);
end;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.