Mega Code Archive

 
Categories / Delphi / VCL
 

Freeing an object stored in a list

Question: When I add an object to a TStrings component, how should I free the object? Answer: Simply call the objects free method. The following example attaches a icon object to the strings property of a listbox during the forms creation, and frees the icon when the form is destroyed. Example: procedure TForm1.FormCreate(Sender: TObject); var Icon: TIcon; begin Icon := TIcon.Create; Icon.LoadFromFile('C:\Program Files\BorlandImages\CONSTRUC.ICO'); ListBox1.Items.AddObject('Item 0', Icon); end; procedure TForm1.FormDestroy(Sender: TObject); begin ListBox1.Items.Objects[0].Free; end;