?
Solved

declaring procedures

Posted on 2003-03-31
3
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 13 hours left to enroll

752 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