Jo3
Jo3

Reputation: 3

One procedure for all checks how?

In my form there are 30 or more TCheckbox with TNumberBox in front of them. Every TCheckbox and TNumberBox are connected like CheckBox1 name is C1 and NumberBox is C1E and Checkbox2 is C2 and NumberBox2 is C2E and so on. If C1 is Checked then C1E will be enabled. I don't want to use different onclick events for every Tcheckbox. I just want to use an single procedure for all TCheckbox onclick events. How can I do that ?

Upvotes: 0

Views: 348

Answers (2)

Rudy Velthuis
Rudy Velthuis

Reputation: 28846

You could assign the following OnClick handler (or something similar) to each of the checkboxes:

procedure TYourFormName.CheckBoxClick(Sender: TObject);
var
  Assoc: TControl;
  ChkName: string;
begin
  ChkName := TCheckBox(Sender).Name;                // e.g. 'C1', 'C2', ...
  Assoc := TControl(FindComponent(ChkName + 'E'));  // e.g. 'C1E', 'C2E', ...
  if Assigned(Assoc) then
    Assoc.Enabled := TCheckBox(Sender).Checked;
end;

Upvotes: 6

Sebastian Proske
Sebastian Proske

Reputation: 8413

If you have created those checkboxes and numberboxes at design time, you can use live bindings. Open the LiveBindings Designer and locate your components. Add the visible property to your numberboxes (clicking the triple dots at the bottom of the member). Then connect the IsChecked property of the checkbox with the Visible property of the numberbox (click and drag).

Upvotes: 1

Related Questions