Changing Initial Values

From RAD Studio
Jump to: navigation, search

Go Up to Customizing a grid Index

A calendar is essentially a grid with a fixed number of rows and columns, although not all the rows always contain dates. For this reason, you have not published the grid properties ColCount and RowCount, because it is highly unlikely that users of the calendar will want to display anything other than seven days per week. You still must set the initial values of those properties so that the week always has seven days, however.

To change the initial values of the component's properties, override the constructor to set the desired values. The constructor must be virtual.

Remember that you need to add the constructor to the public part of the component's object declaration, then write the new constructor in the implementation part of the component's unit. The first statement in the new constructor should always be a call to the inherited constructor. Then add the StdCtrls unit to the uses clause.

type
  TSampleCalendar = class(TCustomGrid)
  public
    constructor Create(AOwner: TComponent); override;
    // …
  end;
// …
constructor TSampleCalendar.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);                                 { call inherited constructor }
  ColCount := 7;                                                { always seven days/week }
  RowCount := 7;                                    { always six weeks plus the headings }
  FixedCols := 0;                                                        { no row labels }
  FixedRows := 1;                                                { one row for day names }
  ScrollBars := ssNone;                                              { no need to scroll }
  Options := Options - [goRangeSelect] + [goDrawFocusSelected];  {disable range selection}
end;
//header file
class PACKAGE TSampleCalendar : public TCustomGrid
{
protected:
    virtual void __fastcall DrawCell(int ACol, int ARow, const Windows::TRect &Rect,
      TGridDrawState AState);
    // …
public:
    __fastcall TSampleCalendar(TComponent *Owner);   // the added constructor
    // …
};


//implementation file
__fastcall TSampleCalendar::TSampleCalendar(TComponent *Owner) : TCustomGrid(Owner)
{
  ColCount = 7;
  RowCount = 7;
  FixedCols = 0;
  FixedRows = 1;
  ScrollBars = ssNone;
  Options = (Options >> goRangeSelect) << goDrawFocusSelected;
}
void __fastcall TSampleCalendar::DrawCell(int ACol, int ARow, const Windows::TRect
  &ARect, TGridDrawState AState)
{
}

The calendar now has seven columns and seven rows, with the top row fixed, or nonscrolling.

See Also