1. Home
  2. Computing & Technology
  3. Delphi Programming

How To Create a TButton at Run-Time

By Zarko Gajic, About.com

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:
  1. Declare an instance variable of the component type that you wish to create {e.g. TButton}.
  2. 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.
  3. 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.
  4. 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.
  5. Set any other properties that are necessary (i.e. Width, Height).
  6. Finally, make the component appear on the form by setting the component's Visible property to True.
  7. 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:
  1. Since the button was created with an owner, it will be freed automatically when its owner, the form, is freed.
  2. 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.
  3. 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.
More Delphi Programming How To's
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.