1 //Some components will trigger OnChange, OnClick, etc. events
2 //even if a property is changed programmatically,
3 //not by the user.
4 unit Unit1;
5
6 interface
7
8 uses
9 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
10 Dialogs, StdCtrls,Menus,ComCtrls;
11
12 type
13 TForm1 = class(TForm)
14 CheckBox1: TCheckBox;
15 Button1: TButton;
16 procedure CheckBox1Click(Sender: TObject);
17 procedure Button1Click(Sender: TObject);
18 private
19 { Private declarations }
20 public
21 { Public declarations }
22 end;
23
24 var
25 Form1: TForm1;
26
27 implementation
28
29 {$R *.dfm}
30 //Here are some examples how to avoid these events.
31
32 procedure SetCheckBox(C: TCheckBox; V: Boolean);
33 var
34 N: TNotifyEvent;
35 begin
36 with C do
37 begin
38 N:= OnClick;
39 OnClick:= nil;
40 Checked:= V;
41 OnClick:= N;
42 end;
43 end;
44
45 procedure SetMenuCheck(C: TMenuItem; V: Boolean);
46 var
47 N: TNotifyEvent;
48 begin
49 with C do
50 begin
51 N:= OnClick;
52 OnClick:= nil;
53 Checked:= V;
54 OnClick:= N;
55 end;
56 end;
57
58 procedure TrackPos(C: TTrackBar; V: Integer);
59 var
60 N: TNotifyEvent;
61 begin
62 with C do
63 begin
64 N:= OnChange;
65 OnChange:= nil;
66 Position:= V;
67 OnChange:= N;
68 end;
69 end;
70
71
72 procedure TForm1.CheckBox1Click(Sender: TObject);
73 begin
74 Showmessage('Clicked');
75 end;
76 //Test:
77 procedure TForm1.Button1Click(Sender: TObject);
78 begin
79 SetCheckBox(CheckBox1, True);
80 end;
81
82 end.
|