Mega Code Archive

 
Categories / Delphi / VCL
 

How to set properties without triggering an event

//Some components will trigger OnChange, OnClick, etc. events //even if a property is changed programmatically, //not by the user. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,Menus,ComCtrls; type TForm1 = class(TForm) CheckBox1: TCheckBox; Button1: TButton; procedure CheckBox1Click(Sender: TObject); procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} //Here are some examples how to avoid these events. procedure SetCheckBox(C: TCheckBox; V: Boolean); var N: TNotifyEvent; begin with C do begin N:= OnClick; OnClick:= nil; Checked:= V; OnClick:= N; end; end; procedure SetMenuCheck(C: TMenuItem; V: Boolean); var N: TNotifyEvent; begin with C do begin N:= OnClick; OnClick:= nil; Checked:= V; OnClick:= N; end; end; procedure TrackPos(C: TTrackBar; V: Integer); var N: TNotifyEvent; begin with C do begin N:= OnChange; OnChange:= nil; Position:= V; OnChange:= N; end; end; procedure TForm1.CheckBox1Click(Sender: TObject); begin Showmessage('Clicked'); end; //Test: procedure TForm1.Button1Click(Sender: TObject); begin SetCheckBox(CheckBox1, True); end; end.