Recent

Author Topic: Dynamic Component at runtime  (Read 4428 times)

jobarb

  • Newbie
  • Posts: 3
Dynamic Component at runtime
« on: July 10, 2015, 07:41:36 am »
 I have a form on which I am creating listboxes to display items fetched from a database, each of the listboxes have varying number of items. I create seven listboxes across the screen an start a row increment the width of the list box and create the next listbox etc. when I get to row 2 I need the following if it finds that a listbox height exceeds a fixed height in this case 350 then it must skip buy the width of the listbox and create it at the next column. I use the following code, but I cannot get the cloumns to get created as desired. Could someone please advise me on the best way to do this, thanks in advance. Herewith the code.
Code: [Select]
procedure Tmainfrm.CreateTickets(scount: Integer);
var
 nListbox : TListBox; itemCount, TimeElapsed : integer;
 start, stop, S3 : string;
begin
 itemCount := 0;
 nTicketNO:=TicketsQuery.FieldByName('ticketno').Value;
 nCheckNo:=TicketsQuery.FieldByName('Checkno').Value;
 start := formatDateTime('hh:mm:ss',TicketsQuery.FieldByName('ordertime').Value);
 stop := formatDateTime('hh:mm:ss',Now);
 TimeElapsed := timediffs.TimeDiffs(start,stop);
 S3 := SecToTime(TimeElapsed);
 PrQuery.close;
 PrQuery.SQL.Clear;
 PrQuery.SQL.Add('select * from ticketitems ');
 PrQuery.SQL.Add('where orderno = :orderno');
 PrQuery.SQL.Add('order by lineorderno,itemno');
 PrQuery.Params.Items[0].AsInteger := nTicketNO;
 PrQuery.Open;
 itemCount := PrQuery.RecordCount;

 if (((Scount-1) mod 7) = 0 ) or (lpos >= 1386 ) then
  begin
    inc(tpos,351);  inc(nRow,1);

  if (nrow = 2)  then
   begin
   lPos := 0; //nColumn := 1;
   if (nColumn = 1) and (pos1 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 0;
    if (nColumn = 2) and (pos2 > tpos) then
     inc(lPos,231); //inc(nColumn); //  else lpos := 231;
    if (nColumn = 3) and (pos3 > tpos) then
     inc(lPos,231); //inc(nColumn); //  else lpos := 462;
    if (nColumn = 4) and (pos4 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 693;
    if (nColumn = 5) and (pos5 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 924;
    if (nColumn = 6) and (pos6 > tpos) then
     inc(lPos,231) ; //inc(nColumn); // else lpos := 1155;
    if (nColumn = 7) and (pos7 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 1386;
   end;

   if (nrow = 3 ) then
    begin
     lPos := 0; //nColumn := 1;
     if (nColumn = 1) and ((pos1 > tpos) or (pos8 > tpos)) then
        inc(lPos,231);
     if (nColumn = 2) and ((pos2 > tpos) or (pos9 > tpos)) then
        inc(lPos,231);
     if (nColumn = 3) and ((pos3 > tpos) or (pos10 > tpos)) then
        inc(lPos,231);
     if (nColumn = 4) and ((pos4 > tpos) or (pos11 > tpos)) then
        inc(lPos,231);
     if (nColumn = 5) and ((pos5 > tpos) or (pos12 > tpos)) then
        inc(lPos,231);
     if (nColumn = 6) and ((pos6 > tpos) or (pos13 > tpos)) then
        inc(lPos,231);
     if (nColumn = 7) and ((pos7 > tpos) or (pos14 > tpos)) then
        inc(lPos,231);
    end;
{   if (nrow = 3 ) then
    begin
     lPos := 0; nColumn := 1;
     if (nColumn = 1) and (pos8 > tpos) then
        inc(lPos,231); inc(nColumn);
     if (nColumn = 2) and  (pos9 > tpos) then
        inc(lPos,231); inc(nColumn); //  else
     if (nColumn = 3) and  (pos10 > tpos) then
        inc(lPos,231); inc(nColumn); //  else
     if (nColumn = 4) and (pos11 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 5) and (pos12 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 6) and  (pos13 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 7) and (pos14 > tpos) then
        inc(lPos,231) ; inc(nColumn); // else
    end;    }

   if (nrow = 4 ) then
    begin
     lPos := 0;
    if (nColumn = 1) then
    if pos1 > tpos then
       inc(lPos,231)  else lpos := 231;
     if pos2 > tpos then
      if (nColumn = 2) then
       inc(lPos,231)  else lpos := 462;
     if pos3 > tpos then
      if (nColumn = 3) then
       inc(lPos,231)  else lpos := 693;
     if pos4 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos5 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos6 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos7 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos8 > tpos then

      if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos9 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos10 > tpos then
      if (nColumn = 3) then
       inc(lPos,231) else lpos := 693;
     if pos11 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos12 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos13 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos14 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos15 > tpos then

     if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos16 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos17 > tpos then
      if (nColumn = 3)  then
       inc(lPos,231) else lpos := 693;
     if pos18 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos19 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos20 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos21 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos22 > tpos then
      if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos23 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos24 > tpos then
      if (nColumn = 3) then
       inc(lPos,231) else lpos := 693;
     if pos25 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos26 > tpos then
      if (nColumn = 5) then
      inc(lPos,231) else lpos := 1155;
     if pos27 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos28 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
   end;
    if (nrow = 5 ) then
     begin
    lPos := 0;
       if pos1 > tpos then
        if (nColumn = 1) then
        lpos := 231  else inc(lPos,231);
       if pos2 > tpos then
        if (nColumn = 2) then
        lpos := 462  else inc(lPos,231);
       if pos3 > tpos then
        if (nColumn = 3) then
        lpos := 693  else  inc(lPos,231);
       if pos4 > tpos then
        if (nColumn = 4) then
        lpos := 924  else inc(lPos,231);
       if pos5 > tpos then
        if (nColumn = 5) then
        inc(lPos,231) else lpos := 1155;
       if pos6 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos7 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos8 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos9 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos10 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos11 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos12 > tpos then
        if (nColumn = 5) then
         inc(lPos,231) else lpos := 1155;
       if pos13 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos14 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos15 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos16 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos17 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos18 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos19 > tpos then
        if (nColumn = 5) then
         inc(lPos,231) else lpos := 1155;
       if pos20 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos21 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos22 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos23 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos24 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos25 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos26 > tpos then
        if (nColumn = 5) then
        inc(lPos,231) else lpos := 1155;
       if pos27 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos28 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
     end;
  end
   else
   inc(lPos,231);

   if lpos = 0 then nColumn := 1 else
   if lpos = 231 then nColumn := 2 else
   if lpos = 462 then nColumn := 3 else
   if lpos = 693 then nColumn := 4 else
   if lpos = 924 then nColumn := 5 else
   if lpos = 1155 then nColumn := 6 else
   if lpos = 1386 then nColumn := 7 ;

  nListbox := TListbox.create(self);
  nListbox.name := 'nListbox' + inttostr(scount);
  nListbox.Left := lPos;
  nListbox.top := Tpos;
  nListbox.Height := (itemCount+6)*18;  BoxHeight:= nListbox.Height;

  // Row1
  if (nrow = 1)  then
  begin
   if (nColumn = 1) then
    pos1 := nListbox.Height;
   if (nColumn = 2) then
    pos2 := nListbox.Height;
   if (nColumn = 3) then
    pos3 := nListbox.Height;
   if (nColumn = 4) then
    pos4 := nListbox.Height;
   if (nColumn = 5) then
    pos5 := nListbox.Height;
   if (nColumn = 6) then
    pos6 := nListbox.Height;
   if (nColumn = 7) then
    pos7 := nListbox.Height;
  end;
// Row2
  if (nrow = 2) then
  begin
   if (nColumn = 1) then
    pos8 := nListbox.Height;
   if (nColumn = 2) then
    pos9 := nListbox.Height;
   if (nColumn = 3) then
    pos10 := nListbox.Height;
   if (nColumn = 4) then
    pos11 := nListbox.Height;
   if (nColumn = 5) then
    pos12 := nListbox.Height;
   if (nColumn = 6) then
    pos13 := nListbox.Height;
   if (nColumn = 7) then
    pos14 := nListbox.Height;
  end;
// Row3
  if (nrow = 3) then
  begin
   if (nColumn = 1) then
    pos15 := nListbox.Height;
   if (nColumn = 2) then
    pos16 := nListbox.Height;
   if (nColumn = 3) then
    pos17:= nListbox.Height;
   if (nColumn = 4) then
    pos18 := nListbox.Height;
   if (nColumn = 5) then
    pos19 := nListbox.Height;
   if (nColumn = 6) then
    pos20 := nListbox.Height;
   if (nColumn = 7) then
    pos21 := nListbox.Height;
  end;
  // Row4
  if (nrow = 4) then
  begin
   if (nColumn = 1) then
    pos22 := nListbox.Height;
   if (nColumn = 2) then
    pos23 := nListbox.Height;
   if (nColumn = 3) then
    pos24 := nListbox.Height;
   if (nColumn = 4) then
    pos25 := nListbox.Height;
   if (nColumn = 5) then
    pos26 := nListbox.Height;
   if (nColumn = 6) then
    pos27 := nListbox.Height;
   if (nColumn = 7) then
    pos28 := nListbox.Height;
  end;
  nListbox.Width := 230;
  nListbox.parent := ScrollBox1;
  if  (TimeElapsed/60 >10) and (TimeElapsed/60 < 20) then
   nListbox.Color:=clYellow
   else
  if  (TimeElapsed/60 >20) and (TimeElapsed/60 < 30) then
    nListbox.Color:=$00FCCCFD
  else
  if  TimeElapsed/60 >30 then
   nListbox.Color:=clRed
  else
  nListbox.Color := clWhite;
  nListbox.Font.Color :=clBlack;
  nListbox.Hint:=intToStr(nTicketNO); //+' '+intToStr(nColumn);
  nListBox.ShowHint:=True;
  nListbox.OnDblClick := @LClick;
  nListbox.OnClick:=@nClick;
  nListbox.Font.Name := 'Tahoma';
  nListbox.Font.Size := 11 ;
  nListbox.Items.Clear;
  nListbox.Items.Add('     Table No '+ TableNo.Caption);
  nListbox.Items.Add('        '+ Clerk.Caption);
  nListbox.Items.Add('----------- '+ S3+' -----------');
 if PrQuery.Recordcount > 0 then
  begin
   while not PrQUERY.Eof do
   begin
    if (PrQuery.FieldByName('PartNo').Value = 'Cond') and  (PrQuery.FieldByName('Redprint').Value = 'T' ) then
     begin
      if PrQuery.fieldbyname('Qty').Value > 1 then
       begin
        nListbox.Items.Add('    '+PrQuery.fieldbyname('Qty').asString+' '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString);
       end
      else
       nListbox.Items.Add('    '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString)
      end
    else
      nListbox.Items.Add(PrQuery.fieldbyname('Qty').asString+' '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString);
      PrQuery.next;
    end;
   end;
  PrQuery.close;
end;
if need be I can upload my project for someone to try
« Last Edit: July 10, 2015, 11:04:29 pm by jobarb »

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Dynamic Component at runtime
« Reply #1 on: July 10, 2015, 09:40:35 am »
You have a lot of repeated code.
It's easier to set up some data structures (e.g. arrays) that contain all those hard coded values and use some simple code to loop through those values and do tests accordingly.
Or alternatively set up a procedure for each repeated code block that does the required tests.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Dynamic Component at runtime
« Reply #2 on: July 10, 2015, 10:58:47 am »
It is also far better to decouple the setup and layout of your GUI from code that iterates the query and displays individual items.

In your case you don't really want dynamic listboxes. The layout is fixed. You simply want the listbox items filled in a particular order.

Why not have a routine that creates and displays the 7 (empty) listboxes, completely separate from the routine that scans the data?
Then design a routine that loops through your query putting data in the appropriate listbox (you could hide any listbox that was not required).
What happens if your data count is too great to be accommodated by 7 listboxes?

wildfire

  • Full Member
  • ***
  • Posts: 110
Re: Dynamic Component at runtime
« Reply #3 on: July 10, 2015, 01:49:23 pm »
@OP, I know you're new to the forums, but it would help if you used code tags...

ie (remove *'s ).
[*code]
Your code here.
[*/code]


This benefits others as 1) your code would be in a fixed font, therefor keeping good indentation and 2) it would shorten the length of your post so would reduce scrolling.

eg...

Code: [Select]

procedure Tmainfrm.CreateTickets(scount: Integer);
var
 nListbox : TListBox; itemCount, TimeElapsed : integer;
 start, stop, S3 : string;
begin
 itemCount := 0;

 nTicketNO:=TicketsQuery.FieldByName('ticketno').Value;
 nCheckNo:=TicketsQuery.FieldByName('Checkno').Value;
 start := formatDateTime('hh:mm:ss',TicketsQuery.FieldByName('ordertime').Value);
 stop := formatDateTime('hh:mm:ss',Now);
 TimeElapsed := timediffs.TimeDiffs(start,stop);
 S3 := SecToTime(TimeElapsed);
 PrQuery.close;
 PrQuery.SQL.Clear;
 PrQuery.SQL.Add('select * from ticketitems ');
 PrQuery.SQL.Add('where orderno = :orderno');
 PrQuery.SQL.Add('order by lineorderno,itemno');
 PrQuery.Params.Items[0].AsInteger := nTicketNO; //StrToInt(InvNo.Caption) ; ;
 PrQuery.Open;
 itemCount := PrQuery.RecordCount;

 if (((Scount-1) mod 7) = 0 ) or (lpos >= 1386 ) then
  begin
    inc(tpos,351);  inc(nRow,1);

  if (nrow = 2)  then
   begin
   lPos := 0; //nColumn := 1;
   if (nColumn = 1) and (pos1 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 0;
    if (nColumn = 2) and (pos2 > tpos) then
     inc(lPos,231); //inc(nColumn); //  else lpos := 231;
    if (nColumn = 3) and (pos3 > tpos) then
     inc(lPos,231); //inc(nColumn); //  else lpos := 462;
    if (nColumn = 4) and (pos4 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 693;
    if (nColumn = 5) and (pos5 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 924;
    if (nColumn = 6) and (pos6 > tpos) then
     inc(lPos,231) ; //inc(nColumn); // else lpos := 1155;
    if (nColumn = 7) and (pos7 > tpos) then
     inc(lPos,231); //inc(nColumn); // else lpos := 1386;
   end;

   if (nrow = 3 ) then
    begin
     lPos := 0; //nColumn := 1;
     if (nColumn = 1) and ((pos1 > tpos) or (pos8 > tpos)) then
        inc(lPos,231); //inc(nColumn);
     if (nColumn = 2) and ((pos2 > tpos) or (pos9 > tpos)) then
        inc(lPos,231); //inc(nColumn); //  else
     if (nColumn = 3) and ((pos3 > tpos) or (pos10 > tpos)) then
        inc(lPos,231); //inc(nColumn); //  else
     if (nColumn = 4) and ((pos4 > tpos) or (pos11 > tpos)) then
        inc(lPos,231); //inc(nColumn); // else
     if (nColumn = 5) and ((pos5 > tpos) or (pos12 > tpos)) then
        inc(lPos,231); //inc(nColumn); // else
     if (nColumn = 6) and ((pos6 > tpos) or (pos13 > tpos)) then
        inc(lPos,231); //inc(nColumn); // else
     if (nColumn = 7) and ((pos7 > tpos) or (pos14 > tpos)) then
        inc(lPos,231); //inc(nColumn); // else
    end;
{   if (nrow = 3 ) then
    begin
     lPos := 0; nColumn := 1;
     if (nColumn = 1) and (pos8 > tpos) then
        inc(lPos,231); inc(nColumn);
     if (nColumn = 2) and  (pos9 > tpos) then
        inc(lPos,231); inc(nColumn); //  else
     if (nColumn = 3) and  (pos10 > tpos) then
        inc(lPos,231); inc(nColumn); //  else
     if (nColumn = 4) and (pos11 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 5) and (pos12 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 6) and  (pos13 > tpos) then
        inc(lPos,231); inc(nColumn); // else
     if (nColumn = 7) and (pos14 > tpos) then
        inc(lPos,231) ; inc(nColumn); // else
    end;    }

   if (nrow = 4 ) then
    begin
     lPos := 0;
    if (nColumn = 1) then
    if pos1 > tpos then
       inc(lPos,231)  else lpos := 231;
     if pos2 > tpos then
      if (nColumn = 2) then
       inc(lPos,231)  else lpos := 462;
     if pos3 > tpos then
      if (nColumn = 3) then
       inc(lPos,231)  else lpos := 693;
     if pos4 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos5 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos6 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos7 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos8 > tpos then

      if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos9 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos10 > tpos then
      if (nColumn = 3) then
       inc(lPos,231) else lpos := 693;
     if pos11 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos12 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos13 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos14 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos15 > tpos then

     if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos16 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos17 > tpos then
      if (nColumn = 3)  then
       inc(lPos,231) else lpos := 693;
     if pos18 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos19 > tpos then
      if (nColumn = 5) then
       inc(lPos,231) else lpos := 1155;
     if pos20 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos21 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
     if pos22 > tpos then
      if (nColumn = 1 ) then
       inc(lPos,231) else lpos := 231;
     if pos23 > tpos then
      if (nColumn = 2) then
       inc(lPos,231) else lpos := 462;
     if pos24 > tpos then
      if (nColumn = 3) then
       inc(lPos,231) else lpos := 693;
     if pos25 > tpos then
      if (nColumn = 4) then
       inc(lPos,231) else lpos := 924;
     if pos26 > tpos then
      if (nColumn = 5) then
      inc(lPos,231) else lpos := 1155;
     if pos27 > tpos then
      if (nColumn = 6) then
       inc(lPos,231) else lpos := 1386;
     if pos28 > tpos then
      if (nColumn = 7) then
       inc(lPos,231) else lpos := 1617;
   end;
    if (nrow = 5 ) then
     begin
    lPos := 0;
       if pos1 > tpos then
        if (nColumn = 1) then
        lpos := 231  else inc(lPos,231);
       if pos2 > tpos then
        if (nColumn = 2) then
        lpos := 462  else inc(lPos,231);
       if pos3 > tpos then
        if (nColumn = 3) then
        lpos := 693  else  inc(lPos,231);
       if pos4 > tpos then
        if (nColumn = 4) then
        lpos := 924  else inc(lPos,231);
       if pos5 > tpos then
        if (nColumn = 5) then
        inc(lPos,231) else lpos := 1155;
       if pos6 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos7 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos8 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos9 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos10 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos11 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos12 > tpos then
        if (nColumn = 5) then
         inc(lPos,231) else lpos := 1155;
       if pos13 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos14 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos15 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos16 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos17 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos18 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos19 > tpos then
        if (nColumn = 5) then
         inc(lPos,231) else lpos := 1155;
       if pos20 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos21 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
       if pos22 > tpos then
        if (nColumn = 1 ) then
         inc(lPos,231) else lpos := 231;
       if pos23 > tpos then
        if (nColumn = 2) then
         inc(lPos,231) else lpos := 462;
       if pos24 > tpos then
        if (nColumn = 3) then
         inc(lPos,231) else lpos := 693;
       if pos25 > tpos then
        if (nColumn = 4) then
         inc(lPos,231) else lpos := 924;
       if pos26 > tpos then
        if (nColumn = 5) then
        inc(lPos,231) else lpos := 1155;
       if pos27 > tpos then
        if (nColumn = 6) then
         inc(lPos,231) else lpos := 1386;
       if pos28 > tpos then
        if (nColumn = 7) then
         inc(lPos,231) else lpos := 1617;
     end;
  end
   else
   inc(lPos,231);

   if lpos = 0 then nColumn := 1 else
   if lpos = 231 then nColumn := 2 else
   if lpos = 462 then nColumn := 3 else
   if lpos = 693 then nColumn := 4 else
   if lpos = 924 then nColumn := 5 else
   if lpos = 1155 then nColumn := 6 else
   if lpos = 1386 then nColumn := 7 ;

  nListbox := TListbox.create(self);
  nListbox.name := 'nListbox' + inttostr(scount);
  nListbox.Left := lPos;
  nListbox.top := Tpos;
  nListbox.Height := (itemCount+6)*18;  BoxHeight:= nListbox.Height;

  // Row1
  if (nrow = 1)  then
  begin
   if (nColumn = 1) then
    pos1 := nListbox.Height;
   if (nColumn = 2) then
    pos2 := nListbox.Height;
   if (nColumn = 3) then
    pos3 := nListbox.Height;
   if (nColumn = 4) then
    pos4 := nListbox.Height;
   if (nColumn = 5) then
    pos5 := nListbox.Height;
   if (nColumn = 6) then
    pos6 := nListbox.Height;
   if (nColumn = 7) then
    pos7 := nListbox.Height;
  end;
// Row2
  if (nrow = 2) then
  begin
   if (nColumn = 1) then
    pos8 := nListbox.Height;
   if (nColumn = 2) then
    pos9 := nListbox.Height;
   if (nColumn = 3) then
    pos10 := nListbox.Height;
   if (nColumn = 4) then
    pos11 := nListbox.Height;
   if (nColumn = 5) then
    pos12 := nListbox.Height;
   if (nColumn = 6) then
    pos13 := nListbox.Height;
   if (nColumn = 7) then
    pos14 := nListbox.Height;
  end;
// Row3
  if (nrow = 3) then
  begin
   if (nColumn = 1) then
    pos15 := nListbox.Height;
   if (nColumn = 2) then
    pos16 := nListbox.Height;
   if (nColumn = 3) then
    pos17:= nListbox.Height;
   if (nColumn = 4) then
    pos18 := nListbox.Height;
   if (nColumn = 5) then
    pos19 := nListbox.Height;
   if (nColumn = 6) then
    pos20 := nListbox.Height;
   if (nColumn = 7) then
    pos21 := nListbox.Height;
  end;
  // Row4
  if (nrow = 4) then
  begin
   if (nColumn = 1) then
    pos22 := nListbox.Height;
   if (nColumn = 2) then
    pos23 := nListbox.Height;
   if (nColumn = 3) then
    pos24 := nListbox.Height;
   if (nColumn = 4) then
    pos25 := nListbox.Height;
   if (nColumn = 5) then
    pos26 := nListbox.Height;
   if (nColumn = 6) then
    pos27 := nListbox.Height;
   if (nColumn = 7) then
    pos28 := nListbox.Height;
  end;
// Row5
  if (nrow = 5) then
  begin
   if (nColumn = 1) then
    pos29 := nListbox.Height;
   if (nColumn = 2) then
    pos30 := nListbox.Height;
   if (nColumn = 3) then
    pos31 := nListbox.Height;
   if (nColumn = 4) then
    pos32 := nListbox.Height;
   if (nColumn = 5) then
    pos33 := nListbox.Height;
   if (nColumn = 6) then
    pos34 := nListbox.Height;
   if (nColumn = 7) then
    pos35 := nListbox.Height;
  end;

  nListbox.Width := 230;
  nListbox.parent := ScrollBox1;
  if  (TimeElapsed/60 >10) and (TimeElapsed/60 < 20) then
   nListbox.Color:=clYellow
   else
  if  (TimeElapsed/60 >20) and (TimeElapsed/60 < 30) then
    nListbox.Color:=$00FCCCFD
  else
  if  TimeElapsed/60 >30 then
   nListbox.Color:=clRed
  else
  nListbox.Color := clWhite;
  nListbox.Font.Color :=clBlack;
  nListbox.Hint:=intToStr(nTicketNO); //+' '+intToStr(nColumn);
  nListBox.ShowHint:=True;
  nListbox.OnDblClick := @LClick;
  nListbox.OnClick:=@nClick;
  nListbox.Font.Name := 'Tahoma';
  nListbox.Font.Size := 11 ;
  nListbox.Items.Clear;
//  nListbox.Items.Add('       ***ORDER TICKET***        ');
//  nListbox.Items.Add('---------  '+ intToStr(nTicketNO)+'  ---------');
  nListbox.Items.Add('     Table No '+ TableNo.Caption);
//  nListbox.Items.Add('B ' +nListbox.name+' '+ intToStr(nRow)+' '+ intToStr(BoxHeight));
  nListbox.Items.Add('        '+ Clerk.Caption); //+' '+TimeToStr(elapsed));
  nListbox.Items.Add('----------- '+ S3+' -----------');
 if PrQuery.Recordcount > 0 then
  begin
   while not PrQUERY.Eof do
   begin
    if (PrQuery.FieldByName('PartNo').Value = 'Cond') and  (PrQuery.FieldByName('Redprint').Value = 'T' ) then
     begin
      if PrQuery.fieldbyname('Qty').Value > 1 then
       begin
        nListbox.Items.Add('    '+PrQuery.fieldbyname('Qty').asString+' '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString);
       end
      else
       nListbox.Items.Add('    '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString)
      end
    else
      nListbox.Items.Add(PrQuery.fieldbyname('Qty').asString+' '+PrQuery.fieldbyName('Description').asString+' '+PrQuery.fieldbyName('seatno').asString);
      PrQuery.next;
    end;
   end;
  PrQuery.close;
end;
« Last Edit: July 10, 2015, 01:52:23 pm by wildfire »
A halo is a mere circle, when does it end?

jobarb

  • Newbie
  • Posts: 3
Re: Dynamic Component at runtime
« Reply #4 on: July 10, 2015, 11:06:49 pm »
thanks wildfire, I am new to forums and now i will format my code better in the future

jobarb

  • Newbie
  • Posts: 3
Re: Dynamic Component at runtime
« Reply #5 on: July 10, 2015, 11:16:33 pm »
It is also far better to decouple the setup and layout of your GUI from code that iterates the query and displays individual items.

In your case you don't really want dynamic listboxes. The layout is fixed. You simply want the listbox items filled in a particular order.

Why not have a routine that creates and displays the 7 (empty) listboxes, completely separate from the routine that scans the data?
Then design a routine that loops through your query putting data in the appropriate listbox (you could hide any listbox that was not required).
What happens if your data count is too great to be accommodated by 7 listboxes?
It is also far better to decouple the setup and layout of your GUI from code that iterates the query and displays individual items.

In your case you don't really want dynamic listboxes. The layout is fixed. You simply want the listbox items filled in a particular order.

Why not have a routine that creates and displays the 7 (empty) listboxes, completely separate from the routine that scans the data?
Then design a routine that loops through your query putting data in the appropriate listbox (you could hide any listbox that was not required).
What happens if your data count is too great to be accommodated by 7 listboxes?
Sorry no the layout is not fixed all the listboxes are of varying heights only the width is fixed, so when a new listboxes is created and the are seven listboxes in row one  then i find the height of the first listbox in row 1 and if the height of the lisboxes is greater than the position of next row it must skip column by the width of the listbox. the second box could also be longer than the row position and therefore it must also get skipped. aswell

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Dynamic Component at runtime
« Reply #6 on: July 12, 2015, 02:45:00 pm »
Here's an example of doing the sort of dynamic component positioning of the sort I think you are after.

Create a new project, double-click to generate an OnCreate handler, and complete as follows.
You would adapt the code for different row/column values, and insertion of real rather than dummy data.

Code: [Select]
unit frmDynamicListBox;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Graphics, StdCtrls;

const
  ColWidth = 100;
  LBWidth = 96;
  RowHeight = 100;

type
  TColRange = 0..6; // change these to suit
  TRowRange = 0..4;

  TPosition = record
    L: integer;
    T: integer;
    Full: boolean;
end;

  TGrid = array[TColRange, TRowRange] of TPosition;

{ TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FBtnAddListBox: TButton;
    FGrid: TGrid;
    FNextCol: integer;
    FNextID: integer;
    FNextRow: integer;
    function GetCurrentFull: boolean;
    function GetDummyQueryData: TStrings;
    function GetEmptyCount: integer;
    function GetNextLBPosition: TPoint;
    procedure BtnAddListBoxClick(Sender: TObject);
    procedure DrawBoundaries(Sender: TObject);
    procedure InitialiseGrid;
procedure SetCurrentFull(AValue: boolean);
procedure SetNextCol(AValue: integer);
    property CurrentFull: boolean read GetCurrentFull write SetCurrentFull;
    property EmptyCount: integer read GetEmptyCount;
    property NextCol: integer read FNextCol write SetNextCol;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
var
  h, w: integer;
begin
  Position:=poMainFormCenter;
  OnPaint:=@DrawBoundaries;
  h:=Succ(High(TRowRange))*RowHeight + 10;
  w:=Succ(High(TColRange))*ColWidth + 10;

  FBtnAddListBox:=TButton.Create(Self);
  FBtnAddListBox.Top:=h;
  FBtnAddListBox.Left:=w - FBtnAddListBox.Width - 10;
  FBtnAddListBox.Caption:='Add Listbox';
  FBtnAddListBox.AutoSize:=True;
  FBtnAddListBox.OnClick:=@BtnAddListBoxClick;
  FBtnAddListBox.Parent:=Self;
  Inc(h, FBtnAddListBox.Height + 10);

  Constraints.MinHeight:=h;
  Constraints.MinWidth:=w;
  SetInitialBounds(0, 0, w, h);

  Randomize;
  InitialiseGrid;
end;

function TForm1.GetCurrentFull: boolean;
begin
  if (FNextCol <= High(TColRange)) and (FNextRow <= High(TRowRange)) then
    Result:=FGrid[FNextCol, FNextRow].Full;
end;

procedure TForm1.SetCurrentFull(AValue: boolean);
begin
  if (FNextCol <= High(TColRange)) and (FNextRow <= High(TRowRange)) then
    FGrid[FNextCol, FNextRow].Full:=AValue;
end;

procedure TForm1.SetNextCol(AValue: integer);
begin
if FNextCol=AValue then Exit;
  if (AValue > High(TColRange)) then
    begin
      FNextCol:=0;
      Inc(FNextRow);
    end
  else FNextCol:=AValue;
end;

function TForm1.GetEmptyCount: integer;
var
  c, r: integer;
begin
  Result:=0;
  for c:=0 to High(TColRange) do
    for r:=0 to High(TRowRange) do
      if not FGrid[c,r].Full then
        Inc(Result);
  if (Result < 1) then
    FBtnAddListBox.Enabled:=False;
end;

procedure TForm1.BtnAddListBoxClick(Sender: TObject);
var
  lb: TListBox;
  posn: TPoint;
  h: integer;
  sl: TStrings;
begin
  lb:=TListBox.Create(Self);
  lb.items.Add(Format('Box number %d',[FNextID]));
  sl:=GetDummyQueryData;
  lb.Items.AddStrings(sl);
  sl.Free;

  h:=lb.Count*16;
  if (h > RowHeight) and (FNextRow < High(TRowRange)) then
    FGrid[FNextCol, FNextRow+1].Full:=True;
posn:=GetNextLBPosition;
  lb.SetBounds(posn.x, posn.y, LBWidth, h);
  lb.Parent:=Self;
  CurrentFull:=True;
  Inc(FNextID);
  repeat
    NextCol:=NextCol+1;
  until (EmptyCount < 1) or not FGrid[FNextCol, FNextRow].Full;
  Repaint;
end;

function TForm1.GetDummyQueryData: TStrings;
var
  i: integer;
begin
  Result:=TStringList.Create;
  for i:=0 to random(9) do
    Result.Add(Format('Data #%d',[i]));
end;

function TForm1.GetNextLBPosition: TPoint;
begin
  while FGrid[FNextCol, FNextRow].Full do
   begin
     NextCol:=NextCol + 1;
     if (FNextCol > High(TColRange)) then begin
       Inc(FNextRow);
       NextCol:=0;
     end;
   end;
  if (FNextRow <= High(TRowRange)) then
    Result:=Point(FGrid[FNextCol, FNextRow].L, FGrid[FNextCol, FNextRow].T);
end;

procedure TForm1.InitialiseGrid;
var
  c, r: integer;
begin
  for c:=0 to High(TColRange) do
    for r:=0 to High(TRowRange) do
      begin
        FGrid[c,r].L:=c*ColWidth;
        FGrid[c,r].T:=r*RowHeight;
end;
end;

procedure TForm1.DrawBoundaries(Sender: TObject);
var
  c, r, w, h, x, y: integer;
begin
  Canvas.Pen.Color:=clSilver;
  w:=ColWidth*Succ(High(TColRange));
  h:=RowHeight*Succ(High(TRowRange));
  for c:=0 to High(TColRange) do
    for r:=0 to High(TRowRange) do
      begin
        x:=c*ColWidth;
        y:=r*RowHeight;
        Canvas.Line(x, 0, x, h);
        Canvas.Line(0, y, w, y);
        if FGrid[c,r].Full then
          Canvas.TextOut(x+10,y+RowHeight-16,Format('FULL c=%d, r=%d',[c,r]))
        else Canvas.TextOut(x+10,y+RowHeight-16,Format('EMPTY c=%d, r=%d',[c,r]));
end;
  x:=Succ(High(TColRange))*ColWidth;
  y:=Succ(High(TRowRange))*RowHeight;
  Canvas.Line(x, 0, x, y);
  Canvas.Line(0, y, x, y);
  Height:=y+2;
  Width:=x+2;
end;

end.

 

TinyPortal © 2005-2018