memo text monitoring

hi

say i have a memo on my form
and if the text 'hello' gets typed in how would i make a message box appear

ie

if memo.text = 'hello' then message box 'hello was typed'  

thanks pete

LVL 1
peter2001Asked:
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.

kretzschmarCommented:
??

using the onchange-event
.....
if pos('hello',memo1.text) > 0 then
  showmessage('hello is appeared');
.....

of course is once hello typed and never deleted,
this message comes with every keystroke into the memo


meikl ;-)
DavidBirch2dotComCommented:
If you want to modify what the user is typing into the edit, then try the on key press event

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
 If key in ['A'..'Z'] then
 begin
   beep;
   Key:= #0;
 end;
end;

would stop all capital letters, you could also make it avoid invalid symbols such as %^&
peter2001Author Commented:
hi

the problem is the text in the memo does not get deleted so how would i go about
showing a message only once as like you say the message pops up every time you press a key in the memo after the word hello is typed

thanks pete




CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

DavidBirch2dotComCommented:
The best option i can think of would be to have a word count function, then to save the count of the number of times a word appears in the memo and then show the message if that changes
DavidBirch2dotComCommented:
Var
  WordCountformemo1: integer;

Function WordCount(Word,text: string): integer;
begin
  result:= 0;
  while pos(Word,text)<>0 do
  begin
     Result := Result+1;
     Delete(text,1,pos(Word,text)+length(Word)-1);
  end;
end;

procedure TForm1.Memo1Change(Sender: TObject);
begin
   If WordCountformemo1<>WordCount('hello',memo1.Text) then
   begin
      WordCountformemo1:= WordCount('hello',memo1.Text);
      ShowMessage('A new hello has been found');
   end;
end;

Alternativly if you dont want to use a varible you could always use the memo1.tag propert if you dont use it for anything else

David

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
kretzschmarCommented:
>how would i go about
>showing a message only once

the hazardous way:

procedure TForm1.Memo1Change(sender : TObject); //just from head, header could sound like this
begin
  if pos('hello',memo1.text) > 0 then
  begin  
    showmessage('hello is appeared');
    Memo1.onChange := NIL;  //unHook this event -> don't forget if you will start new to reassign it
  end;
end;

the way i would use:

var
  message_showed : Boolean := False;  //a local unit-global var,
//could be also in the TForm Class definiton, in case of using mutliple instances in one app

procedure TForm1.Memo1Change(sender : TObject); //just from head, header could sound like this
begin
  if not message_showed then
    if pos('hello',memo1.text) > 0 then
    begin  
      showmessage('hello is appeared');
      message_showed := true;  //-> don't forget if you will start new to reset it to false
    end;
end;

meikl ;-)
DavidBirch2dotComCommented:
Thanks for the points, glad I could help
peter2001Author Commented:
thanks for that

thats was just what i wanted

thankyou

pete
peter2001Author Commented:
hi sorry to bother you could you help me on 1 more thing with this

i have been tring to read from a flatedit box

procedure TForm1.Memo1Change(Sender: TObject);
begin
   If WordCountformemo1<>WordCount(flatedit1.text,memo1.Text) then
   begin
      WordCountformemo1:= WordCount(flatedit1.text,memo1.Text);
      ShowMessage('A new hello has been found');
   end;
end;

it runs ok but does not make a popup message nothing

any ideas why?
DavidBirch2dotComCommented:
I dont seem to have a TFlatEdit in Delphi 7, however I did test the code with a normal edit and it worked correctly, the only thing I did notice is that the code is case sensitive, could this be causeing your problem?  the following version is not case sensitive

Function WordCount(Word,text: string): integer;
begin
  result:= 0;
  while pos(uppercase(Word),uppercase(text))<>0 do
  begin
     Result := Result+1;
     Delete(text,1,pos(Word,text)+length(Word)-1);
  end;
end;

David
peter2001Author Commented:
hi
yeah the flatedit did not work but the normal edit worked ok

i will start another question so i can give you more points to help me sort this out
here is link to it

 http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21481810.html


as i want to check for about 7 different words i thought i would be able to do it like this

Function WordCount(Word,text: string): integer;
begin
  result:= 0;
  while pos(uppercase(Word),uppercase(text))<>0 do

  begin
     Result := Result+1;
     Delete(text,1,pos(Word,text)+length(Word)-1);
  end;
end;

Function WordCount1(Word,text: string): integer;
begin
  result:= 0;
  while pos(uppercase(Word),uppercase(text))<>0 do

  begin
     Result := Result+1;
     Delete(text,1,pos(Word,text)+length(Word)-1);
  end;
end;


procedure TKeylogger.Memo1Change(Sender: TObject);
begin
   If WordCountformemo1<>WordCount(edit1.text,memo1.Text) then
   begin
      WordCountformemo1:= WordCount(edit1.text,memo1.Text);
      ShowMessage('A new '+edit1.text+' has been found');
           end;
            begin
      If WordCountformemo1<>WordCount1(edit2.text,memo1.Text) then
   begin
      WordCountformemo1:= WordCount1(edit2.text,memo1.Text);
      ShowMessage('A new '+edit2.text+' has been found');

            end;

             end;
            end;

but this then soon as 1 word is found it just repeats the popup messasge's . its fine if i just check the 1 word
would it be better to have the words in list seperated by commas as am not sure how to do it
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.