Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

declaring procedures

Posted on 2003-03-31
3
Medium Priority
?
253 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:syllvia
  • 3
3 Comments
 
LVL 9

Accepted Solution

by:
mocarts earned 200 total points
ID: 8239006
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
 
LVL 9

Expert Comment

by:mocarts
ID: 8239420
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
 
LVL 9

Expert Comment

by:mocarts
ID: 8239488
sorr, must be:

procedure TForm1.Button1Click(Sender: TObject);
begin
  MySearch(Richedit1, Edit1.Text);
end;
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question