Creating a Component with the Component Wizard

From RAD Studio
Jump to: navigation, search

Go Up to Creating a New Component


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

  • The personality (Delphi or C++) (note that Win32 is the only choice for framework/platform)
  • The class from which the component is derived
  • The class name for the new component
  • The Tool palette category where you want the component to appear
  • The name of the unit in which the component is created
  • The search path where the unit is to be found
  • The name of the package in which you want to place the component

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

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


To add a new component with the New Component Wizard

  1. To start the New Component wizard, choose one of these two methods:
    • Choose Component > New Component.
    • Choose File > New > Other, go to the Delphi Projects > Delphi Files page and double-click Component.

    Note: The wizard started with File > New > Other cannot add components to an existing unit. You must add components to existing units manually.

  2. Fill in the fields in the New Component wizard.
  3. On the Personality, Framework and Platform page, select the type of component you are creating: Delphi or C++. (If a project is open in the IDE, the personality of your project is used.)
    • On the Ancestor Component page, select the class from which you are deriving your new component.
    • On the Component page, 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 name field, specify the name of the unit you want the component class declared in. If the unit is not on the default search path, edit the Search Path field as necessary.
  4. After you fill in the fields in the New Component wizard, click Finish. To place the component in a new or existing package, click Component > Install and use the dialog box that appears to specify a package.
  5. Click OK. The IDE creates a new unit.

Warning: If you derive a component from a class whose name begins with "TCustom" (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.

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. To see the source code for your unit, either click View > Units... or open the unit file in the Code Editor by selecting File > Open.

The unit looks like this:

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]);
end;
end.


//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


//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);
  }
}

Topics

See Also