Make all editable controls on a form readonly lazarus / 31-LCL ---------------------------------------------------------------------- johnmc · 2022-08-30 12:46 Is there a way of making all the controls on a form with readonly property be set to True or False other than writing a procedure that lists every control and set its ReadOnly property to True or False? something along the lines of : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i := 0 to ComponentCount - 1 do    Components[i].ReadOnly := True;  John ---------------------------------------------------------------------- Bart · 2022-08-30 14:21 You could check wether the control is a TCustomEdit (as in: the is operator)or any other class that is relevant to your application? Bart ---------------------------------------------------------------------- Zvoni · 2022-08-30 14:23 Something like this? https://lazarus- ccr.sourceforge.io/docs/lcl/controls/twincontrol.controls.html TForm inherits through the Chain from TWinControl https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tform.html ---------------------------------------------------------------------- howardpc · 2022-08-30 19:22 Something such as this (on the lines Bart suggests) will do what you are after: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetAppropriateControlsReadOnly(aForm: TForm; aSetToTrue: Boolean);var  c: TComponent;  ce: TCustomEdit absolute c;  i: Integer;begin  for i := 0 to aForm.ControlCount-1 do    begin      c := aForm.Controls[i];      if c.InheritsFrom(TCustomEdit) then        if aSetToTrue then        ce.ReadOnly := True        else ce.ReadOnly := False;    end;end;Note that most controls do not have a ReadOnly property (it is not a property of TControl) so if you want to test also one of the few controls apart from edits that have  this property (such as TComboBox) you have to test explicitly for that class in addition to TCustomEdit. ---------------------------------------------------------------------- mas steindorff · 2022-08-31 01:11 or just place all of the controls you want to control this way on a panel and make it (not enabled) . I make it a habit to place a tpanel down on any form to make coping /duplication simpler. ---------------------------------------------------------------------- KodeZwerg · 2022-08-31 05:18 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Rtti,  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    Memo1: TMemo;    procedure FormCreate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} function SetProperty(const AControl: TControl; const AProperty: string; const AValue: TValue): Boolean;var  LControl: TControl;  LRttiContext: TRttiContext;  LRttiProperty: TRttiProperty;begin  Result := False;  try    LControl := AControl;  LRttiProperty := LRttiContext.GetType(LControl.ClassType).GetProperty(AProperty);    if ((LRttiProperty <> nil) and (LRttiProperty.Visibility in [mvPrivate, mvProtected, mvPublic, mvPublished])) then    begin      LRttiProperty.SetValue(LControl, AValue);      Result := True;    end;  except  end;end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  i: Integer;begin  for i := 0 to Pred(ComponentCount) do    SetProperty(TControl(Components[i]), 'ReadOnly', True);end; end. Works with all. Enjoy. ---------------------------------------------------------------------- Zvoni · 2022-08-31 06:18 Quote from: howardpc on August 30, 2022, 09:22:59 pmSomething such as this (on the lines Bart suggests) will do what you are after:Howard, why not just: saves a few lines, checks, cycles Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetAppropriateControlsReadOnly(aForm: TForm; aSetToTrue: Boolean);var  c: TComponent;  ce: TCustomEdit absolute c;  i: Integer;begin  for i := 0 to aForm.ControlCount-1 do    begin      c := aForm.Controls[i];      if c.InheritsFrom(TCustomEdit) then ce.ReadOnly:=aSetToTrue;  //Zvoni's Edit        //if aSetToTrue then        //  ce.ReadOnly := True        //else ce.ReadOnly := False;    end;end; ---------------------------------------------------------------------- johnmc · 2022-08-31 10:21 Thanks to everyone for the suggestions and help. I will look into these and see if I can get them to do what I need. Regards John ---------------------------------------------------------------------- Ten_Mile_Hike · 2022-10-23 01:30 Guys,  I'm just a hobbyist and don't have the background you all have, but what is wrong with:  Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender:TObject);var i:integer;begin For i := 0 to ComponentCount - 1 do  if (components[i]).inheritsfrom(Tcustomedit)     then (components[i] as Tcustomedit).ReadOnly := True;end; I've tested it and it works fine. No C or Ce variables need to be declared and no "Absolute" needs called ---------------------------------------------------------------------- howardpc · 2022-10-23 06:29 There is nothing "wrong" with such code. In programming you often have multiple ways to achieve your goal, and you pick the one that suits you best. Your code could be safely shortened to a direct cast: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  i: Integer;begin  for i := 0 to ComponentCount - 1 do        if Components[i].InheritsFrom(TCustomEdit) then      TCustomEdit(Components[i]).ReadOnly := True;end;Since you filter out all non-TCustomEdit components there is no chance of mis-casting Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Components[i] which would raise an exception. So the extra test in "TComponents as TCustomEdit" is not needed. ---------------------------------------------------------------------- ASerge · 2022-10-23 10:43 Or Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure SetEditsReadOnly(Where: TCustomForm; ReadOnly: Boolean);var  C: TComponent;begin  for C in Where do    if C is TCustomEdit then      TCustomEdit(C).ReadOnly := ReadOnly;end;