RAD Studio |  |

Creating and registering the component (Grid)

You create every component the same way: create a unit, derive a component class, register it, compile it, and install it on the Tool palette. Creating a new component.

For this example, follow the general procedure for creating a component, with these specifics:

1. Save the component's unit as CalSamp.
2. Derive a new component type called TSampleCalendar, descended from TCustomGrid.
3. Register TSampleCalendar on the Samples category of the Tool palette.

The resulting unit descending from TCustomGrid in a VCL application should look like this:
Copy Code

    unit CalSamp;
    interface
    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids;
    type
      TSampleCalendar = class(TCustomGrid)
      end;
    procedure Register;
    implementation
    procedure Register;
    begin
      RegisterComponents('Samples', [TSampleCalendar]);
    end;
    end.

Copy Code

    #include <vcl\vcl.h>
    #pragma hdrstop
    #include "CalSamp.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init);
    //---------------------------------------------------------------------------
    static inline TSampleCalendar *ValidCtrCheck()
    {
      return new TSampleCalendar(NULL);
    }
    //---------------------------------------------------------------------------
    namespace Calsamp
    {
      void __fastcall PACKAGE Register()
      {
        TComponentClass classes[1] = {__classid(TSampleCalendar)};
        RegisterComponents("Samples", classes, 0); //Use a different page in CLX applications
      }
    }

Copy Code

    #ifndef CalSampH
    #define CalSampH
    //---------------------------------------------------------------------------
    #include <vcl\sysutils.hpp>
    #include <vcl\controls.hpp>
    #include <vcl\classes.hpp>
    #include <vcl\forms.hpp>
    #include <vcl\grids.hpp>
    //---------------------------------------------------------------------------
    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    private:
    protected:
    public:
    __published:
    };
    //---------------------------------------------------------------------------
    #endif

If you install the calendar component now, you will find that it appears on the Samples category. The only properties available are the most basic control properties. The next step is to make some of the more specialized properties available to users of the calendar.

Note: While you can install the sample calendar component you have just compiled, do not try to place it on a form yet. The TCustomGrid component has an abstract DrawCell method that must be redeclared before instance objects can be created. Overriding the DrawCell method is described in Filling in the cells

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