Author: Tomas Rutkauskas
When showing a form with checkboxes connected to a database table it seems the
OnClick event of the checkboxes is being run when the form is shown but before
OnActivate is run. Does anyone know what can cause this? I have code in the OnClick
to enable certain other controls etc. and I am having to disable them conditionally
if the checkbox has focus.
Answer:
Unfortunately the OnClick event executes when the value of the checkbox changes,
not just when the object is "clicked". So if you scroll from a record that is True
to one that is False, the event will be triggered. This is because the component
sets the State of the check box after each record is scrolled. That in turn fires
the Click event. You can override this behavior by subclassing from TDBCheckBox and
override the property ClicksDisabled which is defined in TButtonControl. You'll
also need to override the WndProc method and reset the var FClicksDisabled to True.
This should keep the OnClick executing only when the mouse is clicked and not when
the data is scrolled to a new value. I have no idea why the CheckBox was
implemented this way, it really doesn't make much sense to me.
Here's one that should only fire the onClick event when the mouse is clicked.
1 { ... }2 type3 TMyDBCheckBox = class(TDBCheckBox)
4 private5 { Private declarations }6 FClickOK: Boolean;
7 protected8 { Protected declarations }9 procedure WndProc(varmessage: TMessage); override;
10 procedure Click; override;
11 public12 { Public declarations }13 constructor Create(AOwner: TComponent); override;
14 published15 { Published declarations }16 end;
17 18 procedureregister;
19 20 implementation21 22 procedure TMyDBCheckBox.WndProc(varmessage: TMessage);
23 begin24 inherited;
25 casemessage.Msg of26 WM_LBUTTONDOWN, WM_LBUTTONDBLCLK:
27 FClickOK := True;
28 end;
29 end;
30 31 constructor TMyDBCheckBox.Create(AOwner: TComponent);
32 begin33 inherited;
34 FClickOK := False;
35 end;
36 37 procedure TMyDBCheckBox.Click;
38 begin39 try40 if FClickOK then41 inherited;
42 finally43 FClickOK := False;
44 end;
45 end;
46 47 procedureregister;
48 begin49 RegisterComponents('Custom', [TMyDBCheckBox]);
50 end;