RAD Studio |  |

Storing the Internal Date

To store the date for the calendar, you need a private field to hold the date and a runtime-only property that provides access to that date.

Adding the internal date to the calendar requires three steps:

1. Declare a private field to hold the date:

Copy Code

    type
      TSampleCalendar = class(TCustomGrid)
      private
        FDate: TDateTime;
      .
      .
      .

Copy Code

    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    public:
        __property TDateTime CalendarDate = {read=FDate, write=SetCalendarDate, nodefault};
        .
        .
        .
    };

Copy Code

    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    private:
        TDateTime FDate;
        .
        .
        .
    };

1. Initialize the date field in the constructor:

Copy Code

    constructor TSampleCalendar.Create(AOwner: TComponent);
    begin
      inherited Create(AOwner);         { this is already here }
      .                                  { other initializations here }
      .
      .
      FDate := Date;                    { get current date from RTL }
    end;

Copy Code

    __fastcall TSampleCalendar::TSampleCalendar(TComponent *Owner) : TCustomGrid(Owner)
    {
      .
      .
      .
      FDate = FDate.CurrentDate();
    }

1. Declare a runtime property to allow access to the encoded date. You'll need a method for setting the date, because setting the date requires updating the onscreen image of the control:

Copy Code

    type
      TSampleCalendar = class(TCustomGrid)
      private
        procedure SetCalendarDate(Value: TDateTime);
      public
        property CalendarDate: TDateTime read FDate write SetCalendarDate;
      .
      .
      .
    procedure TSampleCalendar.SetCalendarDate(Value: TDateTime);
    begin
      FDate := Value;                { set new date value }
      Refresh;                       { update the onscreen image }
    end;

Copy Code

    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    private:
        void __fastcall SetCalendarDate(TDateTime Value);
        .
        .
        .
    };

Copy Code

    void __fastcall TSampleCalendar::SetCalendarDate(TDateTime Value)
    {
      FDate = Value;                      // Set the new date value
      Refresh();                          // Update the onscreen image
    }

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