Recent

Author Topic: TGroupBox Usage AddControl Method  (Read 3927 times)

kevin.black

  • Full Member
  • ***
  • Posts: 121
TGroupBox Usage AddControl Method
« on: March 18, 2013, 03:58:33 am »
Hi,

I need to use a TGroupBox with buttons (haven't determined which type yet), dynamically added and removed.  I see the Addcontrol and Removecontrol methods, but I need some advice on how to actually use these methods (yes I did search, but found nothing useful).  This bit of test code, just to see if I could index the controls,  changes the caption of three speedbuttons that I placed on a Groupbox and works fine, ie. exactly what I hoped it would do. 

What I need to do is add and remove the buttons in code:

Code: [Select]
procedure TfrmDLLTest.FormCreate(Sender: TObject);
var
  i: integer;
begin
  for i := 0 to 2 do
  groupbox1.Controls[i].caption := inttostr(i);
end;   
   

Can someone please provide a code snippet using GroupBox.AddControl and GroupBox.RemoveControl (or point me to some adequate documentation)?

And, how do you know which control you are removing since I cannot see anywhere to put an index.  If the syntax was Groupbox1.Removecontrol.Controls[index] then that would make sense, but I do not think it is?

 Any help appreciated....

Thanks,
Kevin

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: TGroupBox Usage AddControl Method
« Reply #1 on: March 18, 2013, 04:28:48 am »
controls is a collection which is maintained by the parent property, to add any control on the controls property of the groupbox or control for that matter you just the controls parent to the groupbox.

add controls to groupbox;

Code: [Select]
Myspeedbutton1.parent := MyGroupBox;
Myspeedbutton2.parent := MyGroupBox;
Myspeedbutton3.parent := MyGroupBox;

remove controls from groupbox;
Code: [Select]
Myspeedbutton1.parent := nil;
Myspeedbutton2.parent := nil;
Myspeedbutton3.parent := nil;

the methods you are refering are used only for controls create in the groupbox constructor and have nothing to do with the controls list at all just forget about them for your purposes.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

kevin.black

  • Full Member
  • ***
  • Posts: 121
Re: TGroupBox Usage AddControl Method
« Reply #2 on: March 18, 2013, 05:19:20 am »
Hi Taazz,

Thank you for your response and I will (attempt) to try what you have advised.

I do note that:

Code: [Select]
groupbox1.RemoveControl(groupbox1.Controls[0]);
Removes the first control in my groupbox if I put that code in button onclick event.  Likewise if I loop through that code it will remove all the controls (if say I had three controls, from 0..2) then

Code: [Select]
for index := 0 to 2 do
  groupbox1.RemoveControl(groupbox1.Controls[index]);

Removes the three controls.

Regardles, I will give usage of the parent property a try, and thanks again.

Regards,
Kevin

 

TinyPortal © 2005-2018