General steps to adding a new control (TButton) to the current form at run-time in Delphi.
Difficulty: Average
Time Required: 10 minutes
Here's How:
- Declare an instance variable of the component type that you wish to create {e.g. TButton}.
- Use the component's Create constructor to create an instance of the component and assign the instance to the instance variable declared in step 1.
- Assign a parent to the component's Parent property (e.g. Form1, Panel1, etc). The Parent determines where the component will be displayed, and how the component's Top and Left coordinates are interpreted.
- To place a component in a groupbox, set the component's Parent property to the groupbox. For a component to be visible, it must have a parent to display itself within.
- Set any other properties that are necessary (i.e. Width, Height).
- Finally, make the component appear on the form by setting the component's Visible property to True.
- If you created the component with an owner, you don't need to do anything to free the component - it will be freed when the owner is destroyed
Tips:
- Since the button was created with an owner, it will be freed automatically when its owner, the form, is freed.
- Changing the parent of a control moves the control onscreen so that it is displayed within the new parent. When the parent control moves, the child moves with the parent.
- Some controls (such as ActiveX controls) are contained in non-VCL windows rather than in a parent control. For these controls, the value of Parent is nil and the ParentWindow property specifies the non-VCL parent window.

