Jump to content

Dynamically array component creation


Skepsis IT

Recommended Posts

Hi everybody,

 

I face a very strange problem, I create some components dynamically using dynamic array using the same way for all of it (unilabel,unidatetimepicker and uniedit).

 

For example:

      datearray[I]:=TUniDateTimePicker.Create(self);
      datearray[I].Parent:=FldsPanel;
      datearray[I].Width:=150;
      datearray[I].Left:=left;
      datearray[I].Top:=top;

of course, above code goes in a for loop...

 

I free all of them the same way

 

For example:

  for cnt := Low(datearray) to High(datearray) do
  begin
    datearray[cnt].Free;
    datearray[cnt] := nil;
  end;

The strange thing is that unidatetimepicker and unilabel array free up ok, uniedit array don't and of course I have an error when I close the form that contains them.

Do you have any clue?

 

Tip: when I debug, I see that when I create components on the other arrays gets value (eg. $xxxxxxxx) but on tuniedit array gets nil.

Link to comment
Share on other sites

Hi skepsis.
 
 
First try this:

 

datearray:=TUniDateTimePicker.Create(self);

 

specify nil
instead self

datearray[I] := TUniDateTimePicker.Create(NIL);
or
datearray[I] := TUniEdit.Create(NIL);
...
 
Secondly: when (where) you destroy objects?
 
Sincerely
Link to comment
Share on other sites

Hi, and thanks for your replies!

 

lema, I'll try to prepare a test case for upload.

 

Delphi developer, I have already tried nil before posting in the forum and I got a Fatal Error: Control can not be constructed with a nil Owner, actually nil not proper for controls on a form.

 

I've made a procedure that destroys the object, firstly I call this procedure at forms OnClose event, but for test purposes I add a button that calls it. Same results.

Link to comment
Share on other sites

Lema, I started a test case and I realize that all the uniedit objects that are created after unidatetimepicker objects cannot be deleted.

I paste a piece of code for better understanding

    if (Grd.Columns[I].Field.DataType=ftDate) or (Grd.Columns[I].Field.DataType=ftDateTime) then
    begin
      inc(datecnt);
      SetLength(datearray,datecnt);
      datearray[I]:=TUniDateTimePicker.Create(self);
      datearray[I].Parent:=FldsPanel;
      datearray[I].Width:=150;
      datearray[I].Left:=left;
      datearray[I].Top:=top;
      datearray[I].TabOrder:=I;
    end
    else
    begin
      inc(editcnt);
      SetLength(editarray,editcnt);
      editarray[I]:=TUniEdit.Create(self);
      editarray[I].Name:='UniEdit'+inttostr(editcnt);
      editarray[I].Text:=inttostr(editcnt);
      editarray[I].Parent:=FldsPanel;
      editarray[I].Width:=150;
      editarray[I].Left:=left;
      editarray[I].Top:=top;
      editarray[I].TabOrder:=I;
    end;

Link to comment
Share on other sites

procedure TMainForm.CreateComponents(Grd: TUniDBGrid);

var

  I,editcnt,datecnt,labelcnt,top,fldcnt: Integer;

begin

  labelcnt:=0;

  datecnt:=0;

  editcnt:=0;

  top:=50;

  left:=16;

  fldcnt:=0;

  for I := 0 to Grd.Columns.Count - 1 do

  begin

    inc(labelcnt);

    SetLength(labels,labelcnt);

    labels:=TUniLabel.Create(self);

    labels.Parent:=FldsPanel;

    labels.Top:=top-19;

    labels.Left:=left;

    labels.Caption:=Grd.Columns.Title.Caption;

    if (Grd.Columns.Field.DataType=ftDate) or (Grd.Columns.Field.DataType=ftDateTime) then

    begin

      inc(datecnt);

      SetLength(datearray,datecnt);

      datearray[datecnt-1]:=TUniDateTimePicker.Create(self);

      datearray[datecnt-1].Parent:=FldsPanel;

      datearray[datecnt-1].Width:=150;

      datearray[datecnt-1].Left:=left;

      datearray[datecnt-1].Top:=top;

      datearray[datecnt-1].TabOrder:=I;

    end

    else

    begin

      inc(editcnt);

      SetLength(editarray,editcnt);

      editarray[editcnt-1]:=TUniEdit.Create(self);

      editarray[editcnt-1].Name:='UniEdit'+inttostr(editcnt);

      editarray[editcnt-1].Parent:=FldsPanel;

      editarray[editcnt-1].Width:=150;

      editarray[editcnt-1].Left:=left;

      editarray[editcnt-1].Top:=top;

      editarray[editcnt-1].TabOrder:=I;

    end;

    left:=left+156;

    inc(fldcnt);

    // УФБ 5 РЕДЙБ КБФЕВБЙНЩ ГСБММЗ

    if fldcnt = 5 then

    begin

      top:=top+56;

      fldcnt:=0;

      left:=16;

    end;

  end; // end of for I

end;

Link to comment
Share on other sites


for I := 0 to Grd.Columns.Count - 1 do
begin
inc(labelcnt);
SetLength(labels,labelcnt);
labels[I]:=TUniLabel.Create(self);
labels[I].Parent:=FldsPanel;
labels[I].Top:=top-19;
labels[I].Left:=left;
labels[I].Caption:=Grd.Columns[I].Title.Caption;
if (Grd.Columns[I].Field.DataType=ftDate) or (Grd.Columns[I].Field.DataType=ftDateTime) then
begin
inc(datecnt);
SetLength(datearray,datecnt);
datearray[datecnt-1]:=TUniDateTimePicker.Create(self);
datearray[datecnt-1].Name:=Grd.Columns[I].FieldName;
datearray[datecnt-1].Parent:=FldsPanel;
datearray[datecnt-1].Width:=150;
datearray[datecnt-1].Left:=left;
datearray[datecnt-1].Top:=top;
datearray[datecnt-1].TabOrder:=I;
end
else
begin
inc(editcnt);
SetLength(editarray,editcnt);
editarray[editcnt-1]:=TUniEdit.Create(self);
editarray[editcnt-1].Name:=Grd.Columns[I].FieldName;
editarray[editcnt-1].Name:='UniEdit'+inttostr(editcnt);
editarray[editcnt-1].Parent:=FldsPanel;
editarray[editcnt-1].Width:=150;
editarray[editcnt-1].Left:=left;
editarray[editcnt-1].Top:=top;
editarray[editcnt-1].TabOrder:=I;
end;
left:=left+156;
inc(fldcnt);
if fldcnt = 5 then
begin
top:=top+56;
fldcnt:=0;
left:=16;
end;
end; // end of for I
end;

Link to comment
Share on other sites

  • 3 years later...

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
×
×
  • Create New...