0

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 ?

Jo3
  • 3
  • 2
  • I guess you could use `FindComponent` after casting `Sender` to `TCheckBox`, getting the name and adding an `'E'`. – Rudy Velthuis Oct 25 '17 at 09:46
  • 1
    Maybe a `TDictionary` to keep track of the pairs? Or use the checkbox's `TComponent.Tag` to store the pointer to the numberbox. – nil Oct 25 '17 at 09:50
  • 3
    Is it 30 of the same thing for something that could be `n`, and you stopped at 30 because *"they should never need more than 30, surely"*? Are design-time controls the right answer at all? This sounds like a job for a list. – J... Oct 25 '17 at 10:04
  • You could create a custom component for this see [this](https://stackoverflow.com/questions/45722476/creating-a-new-component-by-combining-two-controls-tedit-and-ttrackbar-in-delp/45724216#45724216) – Nasreddine Galfout Oct 25 '17 at 17:12
  • I would use a `TFrame` that has 1 `TCheckBox` and 1 `TNumberBox` on it, with on `OnClick` event as needed, and then use 30 instances of that Frame on the Form. – Remy Lebeau Oct 25 '17 at 17:17

2 Answers2

6

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;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
1

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).

Sebastian Proske
  • 8,255
  • 2
  • 28
  • 37
  • I can use live binding but for components more than 30 and I don't know how many they will be cause the program isn't ready yet. – Jo3 Oct 25 '17 at 11:25