Search code examples
delphifiremonkeydelphi-10.2-tokyo

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 ?


Solution

  • 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;