I have a form with several controls where the first one is a TDBCheckBox
that is bound to DataField := 'enabled'
.
When the checkbox is clicked I want all the remaining controls to be enabled / disabled.
procedure TMyAdapter.DataSourceDataChange(Sender: TObject; Field: TField);
var
Enabled: Boolean;
begin
Enabled := FModel.DataSet['enabled'].AsBoolean;
FView.Label1.Enabled := Enabled;
FView.DBEdit1.Enabled := Enabled;
FView.Label2.Enabled := Enabled;
FView.DBEdit2.Enabled := Enabled;
FView.Label3.Enabled := Enabled;
FView.DBEdit3.Enabled := Enabled;
FView.Label4.Enabled := Enabled;
FView.DBEdit4.Enabled := Enabled;
end;
This only works when the focus leaves the checkbox or when the dataset is scrolled (I have a navigator on this form as well).
Is there a way to make the checkbox update its datafield immediately?
Or is there even a better alternative to achieve what I have described?
A couple of problems with using the DataChange event to do things like this are that
It's called a lot more frequently than you actually need, to react to your DBCheckBox being clicked and
Doing a .Post to the dataset is going to change its state, which is generally a bad idea inside an event which can itself be triggered by a change of dataset state.
Ime, it's best to use a standardised way of dealing with these sorts of problems and the one I use is to write a custom message handler that does the work you want, and to call it using PostMessage from your DBCheckBox1Click handler, as shown below: