Board index » delphi » How to initialize "Text"-Property without raising "OnChange"-event

How to initialize "Text"-Property without raising "OnChange"-event

How can I change the contents of a component (i.e. the "Text"-property in the
"TEdit"-class) without getting a "OnChange"(/"OnClick")-event. The problem is
that if I initialize a TEdit-Field a "OnChange"-event will be thrown which
normally has to respond to an useraction.

For example if the "OnChange"-event shows a messagebox this messagebox also
would be shown when the program is started and the TEdit-field is
initialized.

An other problem can occur, if you have two components, that change each
other if a "OnChange"-event is received:

procedure Form1.Edit1OnChange(Sender:TObject);
begin
  Edit2.Text := 'EDIT2: ' + Edit1.Text;
end;

procedure Form1.Edit2OnChange(Sender:TObject);
begin
  Edit1.Text := 'EDIT1: ' + Edit2.Text;
end;

this code will lead to an endless loop.

Robert.Hoe...@t-online.de

 

Re:How to initialize "Text"-Property without raising "OnChange"-event


Quote
Robert.Hoe...@t-online.de (Robert H?lzl) wrote:
>How can I change the contents of a component (i.e. the "Text"-property in the
>"TEdit"-class) without getting a "OnChange"(/"OnClick")-event. The problem is
>that if I initialize a TEdit-Field a "OnChange"-event will be thrown which
>normally has to respond to an useraction.

Var
  MyEdit: TEdit;
  Buffer: PChar;
begin
  ...
  MyEdit.Perform(WM_SETTEXT, 0, Longint(Buffer));
  { or }
  SendMessage(MyEdit.Handle, WM_SETTEXT, 0, Longint(Buffer));
  ...
end;

Share and Enjoy!

+--------------------from usenet----------------------+
|  Anders Bo Melander    | Phone: (+45) 31 87 91 26   |
|  Finsensvej 79, 2. tv. | mailto:a...@biocat.ruc.dk  |
|  DK-2000 Frederiksberg | work: a...@lrsoftware.dk    |
|  Denmark               | flameto:bi...@microsoft.com|
+------------------------+----------------------------+
| http://ftp.frontier.dk/public/Delphi2/tbedit01.html |
+-----------------------------------------------------+

Re:How to initialize "Text"-Property without raising "OnChange"-event


On 5 Mar 1997 22:16:49 GMT, Robert.Hoe...@t-online.de (Robert H?lzl)
wrote:

Quote
>How can I change the contents of a component (i.e. the "Text"-property in the
>"TEdit"-class) without getting a "OnChange"(/"OnClick")-event. The problem is
>that if I initialize a TEdit-Field a "OnChange"-event will be thrown which
>normally has to respond to an useraction.

>For example if the "OnChange"-event shows a messagebox this messagebox also
>would be shown when the program is started and the TEdit-field is
>initialized.

>An other problem can occur, if you have two components, that change each
>other if a "OnChange"-event is received:

>procedure Form1.Edit1OnChange(Sender:TObject);
>begin
>  Edit2.Text := 'EDIT2: ' + Edit1.Text;
>end;

>procedure Form1.Edit2OnChange(Sender:TObject);
>begin
>  Edit1.Text := 'EDIT1: ' + Edit2.Text;
>end;

>this code will lead to an endless loop.

>Robert.Hoe...@t-online.de

Try adding this line before you make the change:
   Edit2.OnChange=nil;

Then after you make your change add this line:
   Edit2.OnChange=Edit2OnChange;

That will turn the event handler off and back on.  BTW, this works for
most events.

Steve Miller
usfmd...@ibmmail.com

Other Threads