You can test the runtime behavior of a component before you install it on the Tool palette. This is particularly useful for debugging newly created components, but the same technique works with any component, whether or not it is on the Tool palette. For information on testing already installed components, see Testing installed components.
You test an uninstalled component by emulating the actions performed by Delphi when the component is selected from the palette and placed on a form.
To test an uninstalled component,
- Add the name of component's unit to the form unit's uses clause.
- Add an object field to the form to represent the component. This is one of the main differences between the way you add components and the way Delphi does it. You add the object field to the public part at the bottom of the form's type declaration. Delphi would add it above, in the part of the type declaration that it manages. Never add fields to the Delphi-managed part of the form's type declaration. The items in that part of the type declaration correspond to the items stored in the form file. Adding the names of components that do not exist on the form can render your form file invalid.
- Attach a handler to the form's OnCreate event.
- Construct the component in the form's OnCreate handler. When you call the component's constructor, you must pass a parameter specifying the owner of the component (the component responsible for destroying the component when the time comes). You will nearly always pass Self as the owner. In a method, Self is a reference to the object that contains the method. In this case, in the form's OnCreate handler, Self refers to the form.
- Assign the Parent property. Setting the Parent property is always the first thing to do after constructing a control. The parent is the component that contains the control visually; usually it is the form on which the control appears, but it might be a group box or panel. Normally, you'll set Parent to Self, that is, the form. Always set Parent before setting other properties of the control.
Warning: If your component is not a control (that is, if TControl is not one of its ancestors), skip this step. If you accidentally set the form's Parent property (instead of the component's) to Self, you can cause an operating-system problem.
- Set any other component properties as desired.