Skepsis IT Posted October 29, 2013 Posted October 29, 2013 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. Quote
lema Posted October 29, 2013 Posted October 29, 2013 Hi ,can you send a simple test case for this issue? Quote
Sherzod Posted October 30, 2013 Posted October 30, 2013 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 Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 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. Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 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; Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 I uploaded a test case. testarray.zip Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 Oops I found it! I use variable I as index to all arrays!!! Firstly thanks for your help and sorry for your loss of time! Quote
Sherzod Posted October 30, 2013 Posted October 30, 2013 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; Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 Yes my friend exactly! Some things are in front of your eyes, but you can't see them. 1 Quote
Skepsis IT Posted October 30, 2013 Author Posted October 30, 2013 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; Quote
lema Posted October 30, 2013 Posted October 30, 2013 Some things are in front of your eyes, but you can't see them. I know , I know. We've all had such a bad day.... Quote
Jancarlos Martins Posted October 30, 2013 Posted October 30, 2013 Hi, I use generics.collections, very simple. http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TList_(Delphi) Quote
rasaliad Posted April 29, 2017 Posted April 29, 2017 Hi, I use generics.collections, very simple. http://docwiki.embarcadero.com/CodeExamples/XE5/en/Generics_Collections_TList_(Delphi) Hi, perjanbr Can you please, give a sample with generics.collections, to create/destroy components at runtime. Thanks, Liriano Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.