Storing the Internal Date

From RAD Studio
Jump to: navigation, search

Go Up to Tracking the 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: type
  TSampleCalendar = class(TCustomGrid)
  private
    FDate: TDateTime;
  .
  .
  . 

class PACKAGE TSampleCalendar : public TCustomGrid
{
public:
    __property TDateTime CalendarDate = {read=FDate, write=SetCalendarDate, nodefault};
    .
    .
    .
}; 
class PACKAGE TSampleCalendar : public TCustomGrid
{
private:
    TDateTime FDate;
    .
    .
    .
};
  1. Initialize the date field in the constructor:

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


    __fastcall TSampleCalendar::TSampleCalendar(TComponent *Owner) : TCustomGrid(Owner)
    {
      .
      .
      .
      FDate = FDate.CurrentDate();
    }
    
  2. 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:

    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; 
    
    class PACKAGE TSampleCalendar : public TCustomGrid
    {
    private:
        void __fastcall SetCalendarDate(TDateTime Value);
        .
        .
        .
    }; 
    void __fastcall TSampleCalendar::SetCalendarDate(TDateTime Value)
    {
      FDate = Value;                      // Set the new date value
      Refresh();                          // Update the onscreen image
    }
    

See Also