Dynamically generated Forms? lazarus / 9-Editor ---------------------------------------------------------------------- forlornity · 2008-01-20 16:41 Hey there, my first post on these forums, I was wondering if any of you could point me in the direction of how on Earth I can add buttons/edit boxes etc to forms on the fly. The reason is that I need to take some information from a MySQL database and make a list of all the information obtained with buttons to do things such as delete etc. Thanks in advance, Forlornity ---------------------------------------------------------------------- gerardus · 2008-01-20 17:24 As easy as: Code: [Select] var   aBtn:TButton; begin   aBtn := TButton.Create(aForm); // By passing the form as a Owner parameter you can forget about freeing it later. Pass Nil if you want to take care of it.   aBtn.Top := 100;   aBtn.Left := 100;   abtn.Caption := 'Hello';   aBtn.Parent := aForm; // Important, otherwisr the button won't show up. end; Regards, Gerard. ---------------------------------------------------------------------- forlornity · 2008-01-20 20:15 Quote from: "gerardus"As easy as: Code: [Select] var   aBtn:TButton; begin   aBtn := TButton.Create(aForm); // By passing the form as a Owner parameter you can forget about freeing it later. Pass Nil if you want to take care of it.   aBtn.Top := 100;   aBtn.Left := 100;   abtn.Caption := 'Hello';   aBtn.Parent := aForm; // Important, otherwisr the button won't show up. end; Regards, Gerard. Thanks very much, I'm pretty new to Delphi and Lazarus, mostly experienced with PHP, Actionscript and C but none of these to any great degree. Check my next post; I have more questions. Thanks, Forlornity ---------------------------------------------------------------------- forlornity · 2008-01-21 11:48 Hey again, I was just wondering how I then delete the buttons (etc) later on, am I missing something glaringly obvious or is it more difficult than I'd think? again, thanks in advance, Forlornity ---------------------------------------------------------------------- theo · 2008-01-21 13:01 There are several way to do this. You could hold a TList or TComponentList (unit contnrs) of all the controls you've inserted or give each control a unique name ( like abtn.Name:='btn_1'; ) and use FindComponent: Code: [Select]procedure TForm1.Button2Click(Sender: TObject); var Comp:TComponent; begin   Comp:=FindComponent('btn_1');   if (Comp<>nil) and (Comp is TControl) then   begin     RemoveControl(TControl(Comp));     Comp.free;   end; end; ---------------------------------------------------------------------- forlornity · 2008-01-21 14:03 Thanks, it was the RemoveControl procedure I needed as I was already storing them as part of an array from which I can find them, also, I let the form handle freeing them, are there any caveats in doing this? Also, is there any way (other than looping through them) of getting rid of all of the buttons when the form they are on is closed so that they don't re-appear when I show it again? thanks again, Forlornity ---------------------------------------------------------------------- gerardus · 2008-01-22 21:53 Quote from: "forlornity"I let the form handle freeing them, are there any caveats in doing this? None. Quote from: "forlornity" Also, is there any way (other than looping through them) of getting rid of all of the buttons when the form they are on is closed so that they don't re-appear when I show it again? I think looping is the only way (Either having them in an array or looping TForm.Components[]). Gerard. ---------------------------------------------------------------------- duncanparsons · 2008-01-24 09:38 Note for speed and efficiency, when iterating the list do something like: Code: [Select] i:=ControlCount-1; while i>=0 do   begin     if controls[i]= then       begin         RemoveControl(controls[i]);       end;     dec(i);   end; Starting from the end of the list and working backwards ensures that you don't move memory around that you don't need to. DSP ---------------------------------------------------------------------- forlornity · 2008-01-24 11:20 Thanks for the help guys, I've got all of this working now thanks to you Now I'm having problems with MySQL though, I'll make a new thread. - Forlornity