Link to home
Start Free TrialLog in
Avatar of peter2001
peter2001

asked on

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

Avatar of kretzschmar
kretzschmar
Flag of Germany image

??

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 ;-)
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

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 %^&
Avatar of peter2001

ASKER

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




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
ASKER CERTIFIED SOLUTION
Avatar of DavidBirch2dotCom
DavidBirch2dotCom

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>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 ;-)
Thanks for the points, glad I could help
thanks for that

thats was just what i wanted

thankyou

pete
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?
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
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

 https://www.experts-exchange.com/questions/21481810/memo-word-monitoring.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