简体   繁体   中英

delphi7 create component from table fields

I want to create components (Panels) on run-time in Delphi 7 from a table, but only create if the field value is > 0.
Example:
widht |p1|p2|p3|p4|p5|p6|p7|..|px|
1500 | 5 | 5 | 5 | 0 | 0 | 0 | 0 |..|0 | // create 3 panels
2700 | 5 | 5 | 5 | 6 | 6 | 0 | 0 |..|0 | // create 5 panels
.......

 private
{ Private declarations }
pn : array of TPanel;
..................................

procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
oldpn: TComponent;
begin
table1.SetKey;
table1.FindNearest([form2.Edit2.Text]);
i:= table1.FieldCount;
SetLength(pn, i);
for i:= 1 to i-1 do
 begin
  oldpn:= Findcomponent('pn'+inttostr(i));
  oldpn.Free;
  pn[i]:= TPanel.Create(form1);
  pn[i].Parent := form1;
  pn[i].Caption := 'Panel' + inttostr(i);
  pn[i].Name := 'pn'+inttostr(i);
  pn[i].Height := table1.Fields[i].Value ;
  pn[i].Width := 500;
  pn[i].Color:=clGreen;
  pn[1].Top := form1.ClientHeight - pn[1].Height;
  if (i > 1) then pn[i].Top := pn[i-1].Top - pn[i].Height;
  pn[i].OnClick := pnClick;
 end;
end;

This code create me the panels but for ALL fields. I want to be able to declare the 'pn' array only from fields with value > 0...
I ve tried:
if table1.Fields[i].Value > 0 then
begin
i:= table1.FieldCount....
but it does't work. Any idea? Thanks in advance!

I would create a second "counter" which keeps track of the number of elements that you're actually setting in the array. I'd set the array to the maximum possible length (your Table1.FieldCount as your have now). Then, once you've looped through all your fields, set the length of the array to that "counter" value.

Something like:

procedure TForm1.btn1Click(Sender: TObject);
var i: integer;
oldpn: TComponent;
count: Integer; // this keeps count of the number of panels you'll be creating
begin
  tbl1.SetKey;
  tbl1.FindNearest([form2.Edit2.Text]);
  i := tbl1.FieldCount;
  SetLength(pn, i);
  count := 0; // initialise count to 0
  for i := 1 to i - 1 do
    if tbl1.fields[1].value > 0 then
    begin
      oldpn := Findcomponent('pn' + inttostr(count));
      oldpn.Free;
      pn[count] := TPanel.Create(form1);
      pn[count].Parent := form1;
      pn[count].Caption := 'Panel' + inttostr(count);
      pn[count].Name := 'pn' + inttostr(count);
      pn[count].Height := tbl1.fields[count].value;
      pn[count].Width := 500;
      pn[count].Color := clGreen;
      pn[0].Top := form1.ClientHeight - pn[0].Height;
      if (count > 0) then
        pn[count].Top := pn[count - 1].Top - pn[count].Height;
      pn[count].OnClick := pnClick;
      inc(count);
    end;

  SetLength(pn, count); // re-adjust length of array
end;

There may be better ways to implement it but this seems simple enough.

Also, with your line:

pn[1].Top := form1.ClientHeight - pn[1].Height;

Are you trying to set it to the bottom of the form? You may be better to change that line to (and the next line too) a single if statement to prevent it being executed every time your loop goes round:

if (count = 0) then
  pn[0].Top := form1.ClientHeight - pn[0].Height;
else
  pn[count].Top := pn[count - 1].Top - pn[count].Height; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM