Need collective "OnChange" handling for several controls lazarus / 31-LCL ---------------------------------------------------------------------- dahaiou · 2012-07-20 10:30 Another thing with the OptionsDialog frame that I asked about the other day: I want to detect whenever any of several controls change, so I can later prompt the user with "Are you sure" if they try to close the dialog without saving changes. I have a general OnChange procedure like this: Code: [Select]procedure TfrmOptionsDialog.myOnChange(Sender: TObject); begin   myOptChangesPending := True; end; Basically I'd like this routine to be triggered by an "OnChange" event whenever _any control_ changes in the dialog (Checkboxes, text fields, radiobuttons or whatever). Is there a way to do it collectively for all controls, or groups of them, instead of setting OnChange for each of them individually? The structure of the dialog is (at present but it might change): Tframe -> TPanel -> TPageControl -> TTabSheet -> TGroupbox -> So, if I could set up an "OnChange" to shoot at anything that moves inside, say the last TGroupbox, that would be great. An "OnChildChange" event if that makes sense. Or any other scheme to achieve a similar result. Is it possible? ---------------------------------------------------------------------- Leledumbo · 2012-07-20 11:03 QuoteIs there a way to do it collectively for all controls, or groups of them, instead of setting OnChange for each of them individually?No, you can only point all of them to the same handler, and checks the Sender parameter for the actual object getting the event. * Actually, I'm thinking whether class helper could help in a situation like this, but since I don't have enough knowledge about the feature, I can't say anything. ---------------------------------------------------------------------- KpjComp · 2012-07-20 11:38 This should do what you want, one word of warning.  The calling Convention for OnChange for all controls will need to be the same,..  Normally this is the case, eg.  TEdit/TCheckBox/TRadioButton etc are simply  OnChange(Sender:TObject); Code: [Select]uses ...,...,typinfo; procedure TForm1.FormCreate(Sender: TObject); // procedure SetChanged(w:TWinControl); var   l:integer;   c:TControl; begin   for l := 0 to w.ControlCount-1 do   begin     c := w.Controls[l];     if IsPublishedProp(c,'OnChange') then     begin       SetMethodProp(c,'OnChange',TMethod(@OnSomethingChange));     end;     if c is TWinControl then       SetChanged(TWinControl(c));   end; end; // begin   SetChanged(self); end; ---------------------------------------------------------------------- dahaiou · 2012-07-20 13:46 Great Stuff. Thanks for that routine. It does exactly what I wanted. I just added a check: Code: [Select]    if ((c as TWinControl).ControlCount > 0) then begin       SetOnChange(c as TWinControl); // recurse here directly for containers     else        SetMethodProp( ...                    // set the method only for childless controls Because I am only interested (at least I think so ?) in detecting "OnChange" events for final nodes  (ie. childless controls such as checkboxes, radiobuttons, text fields etc.), and NOT for the container controls (panels and what have you) earlier in the tree.