![]() |
|
#1
|
|||
|
|||
|
Hello, consider this code:
procedure TForm1.Button1Click(Sender: TObject); var FRec:TSearchrec; ListBox1:TListBox; begin listbox1:=Tlistbox.Create(Self); listbox1.Visible:=false; listbox1.Sorted:=True; ris:=findfirst('c:\*.*',faAnyFile,FRec); while ris=0 do begin listbox1.Items.Add(frec.name); ris:=findnext(FRec); end; findclose(Frec); listbox1.destroy; end; When i execute it, i get an EInvalidOperation exception with message "Control has no parent window". What is wrong? i tried using FORM1 instead of the Self pointer, but still got the same error. |
|
#2
|
|||
|
|||
|
To create a control at runtime, you must set the parent property of the control. So you have to add the command listbox1.parent := self;
P.S. You should never destroy a component with the "destroy" command. Use "free" instead! And to be sure that all allocated memory is released after an possible excepten, you should use a try...finally structure. The correct code should look like this. procedure TForm1.Button1Click(Sender: TObject); var FRec:TSearchrec; ListBox1:TListBox; begin listbox1:=Tlistbox.Create(Self); try listbox1.Visible:=false; listbox1.Sorted:=True; listbox1.Parent := Self; ris:=findfirst('c:\*.*',faAnyFile,FRec); while ris=0 do begin listbox1.Items.Add(frec.name); ris:=findnext(FRec); end; findclose(Frec); finally listbox1.free; end; end; |
|
#3
|
|||
|
|||
|
Just add "listbox1.parent:=form1" after "create"...
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|