ugly code

Discussion of open issues, suggestions and bugs regarding LiteDAC (SQLite Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
vetalkw
Posts: 3
Joined: Fri 19 Mar 2021 09:52

ugly code

Post by vetalkw » Thu 15 Apr 2021 10:30

hello
now I have this code, which I do not like:

Code: Select all

procedure TForm7.Button1Click(Sender: TObject);
  var
    ic:integer;
begin
  i:=Unitable2.FieldCount;
  UniTable2.Append;
  UniTable2.Fields[1].AsString:=Edit2.Text;
  UniTable2.Fields[2].AsString:=Edit3.Text;
  UniTable2.Fields[3].AsString:=Edit4.Text;
  UniTable2.Fields[4].AsString:=Edit5.Text;
  UniTable2.Fields[5].AsString:=Edit6.Text;
  UniTable2.Fields[6].AsString:=Edit7.Text;
  UniTable2.Fields[7].AsString:=Edit8.Text;
  UniTable2.Fields[8].AsString:=Edit9.Text;
  UniTable2.Fields[9].AsString:=Edit10.Text;
  UniTable2.Fields[10].AsString:=Edit11.Text;
  UniTable2.Fields[11].AsString:=Edit12.Text;
  UniTable2.Fields[12].AsString:=Edit13.Text;
  UniTable2.Fields[13].AsString:=Edit14.Text;
  UniTable2.Fields[14].AsString:=Edit15.Text;
  UniTable2.Fields[15].AsString:=Edit16.Text;
  UniTable2.Fields[16].AsString:=Edit17.Text;
  UniTable2.Fields[17].AsString:=Edit18.Text;
  UniTable2.Fields[18].AsString:=Edit19.Text;
  UniTable2.Fields[19].AsString:=Edit20.Text;
  UniTable2.Fields[20].AsString:=Edit21.Text;
  UniTable2.Fields[21].AsString:=Edit22.Text;
  UniTable2.Post;
end;
how do I implement this code in a loop, so that there are fewer lines of code and the code is more beautiful?

instead of: UniTable2.Fields[21].asString I can specify UniTable2.Fields[ic]asString
but I do not know how to specify Edit[ic+1]. Text instead of Edit22.Text?

MaximG
Devart Team
Posts: 1650
Joined: Mon 06 Jul 2015 11:34

Re: ugly code

Post by MaximG » Fri 16 Apr 2021 08:26

You may take a look at this code example in the Delphi documentation: http://docwiki.embarcadero.com/CodeExam ... t_(Delphi)

vetalkw
Posts: 3
Joined: Fri 19 Mar 2021 09:52

Re: ugly code

Post by vetalkw » Fri 16 Apr 2021 19:16

this is what you need!
now please help me with placing my Edits in VertScrollBox2
two questions:
1) how do I make all created Edits visible? (now Visible:=true only for the Edit that is currently being processed in the loop (the other Edits are invisible).
2) how to say that each next Edit should be 10 points lower than the previous one?
the code I use:

Code: Select all

procedure TForm7.TabItem2Click(Sender: TObject);
var
  i, ic, ib: Integer;
const
  NamePrefix = 'MyEdit';
begin
  UniTable1.RecNo:=1+Combobox1.ItemIndex;
  UniTable2.Active:=true;
  ShowMessage ('Unitable2.FieldCount-4='+inttostr(Unitable2.FieldCount-4));
  for i := 1 to Unitable2.FieldCount-4 do begin
    TEdit.Create(Self).Name := NamePrefix + IntToStr(i);
    with TEdit(FindComponent(NamePrefix + IntToStr(i))) do
    begin
      ShowMessage(inttostr(i));
      Parent := VertScrollBox2;
      text:=inttostr(i);
      Left:=10;
      Top:= i * 20;
    end;
  end;
end;
I tried to replace the line:

Code: Select all

      Left:=10;
      Top:= i * 20;
replacement option number 1:

Code: Select all

      TEdit.Create(Self).Margins.Left:=10;
      TEdit.Create(Self).Margins.Top:= i * 20;
replacement option number 2:

Code: Select all

      TEdit(FindComponent(NamePrefix + IntToStr(i))).Margins.Left:=10;
      TEdit(FindComponent(NamePrefix + IntToStr(i))).Margins.Top:= i * 20;
replacement option number 3:

Code: Select all

      TEdit(NamePrefix + IntToStr(i)).Margins.Left:=10;
      TEdit(NamePrefix + IntToStr(i)).Margins.Top:= i * 20;

MaximG
Devart Team
Posts: 1650
Joined: Mon 06 Jul 2015 11:34

Re: ugly code

Post by MaximG » Fri 18 Jun 2021 12:36

The provided error messages are not related to the behavior of our product. Anyway, the issue solution should be found at the specialized subject forums or by contacting Embarcadero support services

Post Reply