Recent

Author Topic: Changing a property in entire project  (Read 2596 times)

ramutau

  • New Member
  • *
  • Posts: 40
Changing a property in entire project
« on: May 12, 2017, 06:38:05 pm »
Hi all,

I have several grids that I have copied from one project to another.

The have a lot of DBEdit components on them, and the datasource in all are now blank.

Is there a way I an 'find and replace' the content of the datasource property without having to manually select each one?

Many thanks


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Changing a property in entire project
« Reply #1 on: May 12, 2017, 08:50:33 pm »
It's not recommended to change an .lfm 'outside' of the IDE, but the following could work.
Declare a private procedure in the form's declaration, and call it in the form's OnCreate as follows.
Once you've run the procedure once, remove the call from the OnCreate, close Lazarus and reopen the project.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   AddSourceToAllDBEditsInLFM('unit1.lfm', DataSource1); // change this to suit your case
  4. end;
  5.  
  6. procedure TForm1.AddSourceToAllDBEditsInLFM(const anLFM: string;
  7.   aDatasource: TDataSource);
  8. var
  9.   sl, slNew: TStringList;
  10.   s: String;
  11.  
  12.   function IsDBEditDefinition(aLine: string): Boolean;
  13.   begin
  14.     aLine:=Trim(aLine);
  15.     Exit((Copy(aLine, 1, 6)='object') and (Copy(aLine, Length(aLine)-6, 7)='TDBEdit'));
  16.   end;
  17.  
  18. begin
  19.   sl:=TStringList.Create;
  20.   slNew:=TStringList.Create;
  21.   try
  22.     sl.LoadFromFile(anLFM);
  23.     for s in sl do begin
  24.       slNew.Add(s);
  25.       if IsDBEditDefinition(s) then
  26.         slNew.Add('    Datasource = ' + aDatasource.Name);
  27.     end;
  28.     slNew.SaveToFile(anLFM);
  29.   finally
  30.     sl.Free;
  31.     slNew.Free;
  32.   end;
  33. end;

 

TinyPortal © 2005-2018