RAD Studio |  |

Creating a Component with the Component Wizard

The Component wizard simplifies the initial stages of creating a component. When you use the Component wizard, you need to specify:

- The class from which the component is derived.
- The class name for the new component.
- The Tool palette category where you want it to appear.
- The name of the unit in which the component is created.
- The search path where the unit is found.
- The name of the package in which you want to place the component.

The Component wizard performs the same tasks you would when creating a component manually:

- Creating a unit.
- Deriving the component.
- Registering the component.

The Component wizard cannot add components to an existing unit. You must add components to existing units manually.
To add a new component with the Component Wizard

1. To start the Component wizard, choose one of these two methods:
   - Choose ComponentNew VCL Component.
   - Choose FileNewOther, goto the Delphi ProjectsDelphi Files page and double-click Component.
2. Fill in the fields in the Component wizard:
   - In the Ancestor Type field, specify the class from which you are deriving your new component.
   - In the Class Name field, specify the name of your new component class.
   - In the Palette Page field, specify the category on the Tool palette on which you want the new component to be installed.
   - In the Unit file name field, specify the name of the unit you want the component class declared in. If the unit is not on the search path, edit the search path in the Search Path field as necessary.
3. After you fill in the fields in the Component wizard, Click Install. To place the component in a new or existing package, click ComponentInstall and use the dialog box that appears to specify a package.
4. Click OK. The IDE creates a new unit.

Warning: If you derive a component from a class whose name begins with "custom" (such as TCustomControl), do not try to place the new component on a form until you have overridden any abstract methods in the original component. Delphi cannot create instance objects of a class that has abstract properties or methods.
To see the source code for your unit, click ViewUnits... (If the Component wizard is already closed, open the unit file in the Code editor by selecting FileOpen.) Delphi creates a new unit containing the class declaration and the Register procedure, and adds a uses clause that includes all the standard Delphi units.

The unit looks like this:

Copy Code

    unit MyControl;
        interface
        uses
          Windows, Messages, SysUtils, Types, Classes, Controls;
        type
          TMyControl = class(TCustomControl)
          private
          { Private declarations }
          protected
          { Protected declarations }
          public
          { Public declarations }
          published
          { Published declarations }
        end;
        procedure Register;
        implementation
        procedure Register;
        begin
          RegisterComponents('Samples', [TMyControl]); //In CLX, use a different page than 'Samples'
        end;
        end.

Copy Code

    //header file
    #ifndef NewComponentH
    #define NewComponentH
    //---------------------------------------------------------------------------
    #include <SysUtils.hpp>
    #include <Controls.hpp>
    #include <Classes.hpp>
    #include <Forms.hpp>
    //---------------------------------------------------------------------------
    class PACKAGE TNewComponent : public TComponent
    {
    private:
    protected:
    public:
    __fastcall TNewComponent(TComponent* Owner);
    __published:
    };
    //---------------------------------------------------------------------------
    #endif

Copy Code

    //implementation file
    #include <vcl.h>
    #pragma hdrstop
    #include "NewComponent.h"
    #pragma package(smart_init);
    //---------------------------------------------------------------------------
    // ValidCtrCheck is used to assure that the components created do not have
    // any pure virtual functions.
    //
    static inline void ValidCtrCheck(TNewComponent *)
    {
    new TNewComponent(NULL);
    }
    //---------------------------------------------------------------------------
    __fastcall TNewComponent::TNewComponent(TComponent* Owner)
    : TComponent(Owner)
    {
    }
    //---------------------------------------------------------------------------
    namespace Newcomponent
    {
    void __fastcall PACKAGE Register()
    {
    TComponentClass classes[1] = {__classid(TNewComponent)};
    RegisterComponents("Samples", classes, 0); //In CLX use a different page than Samples
    }
    }

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!