Adding the ReadOnly property
Go Up to Making the Control Read-only
By adding a ReadOnly property, you will provide a way to make the control read-only at design time. When that property is set to True, you can make all cells in the control unable to be selected.
To add the ReadOnly property, follow these steps:
- Add the property declaration and a private field to hold the value:
Delphi:
type TDBCalendar = class(TSampleCalendar) private FReadOnly: Boolean; { field for internal storage } public constructor Create(AOwner: TComponent); override; { must override to set default } published property ReadOnly: Boolean read FReadOnly write FReadOnly default True; end; // … constructor TDBCalendar.Create(AOwner: TComponent); begin inherited Create(AOwner); { always call the inherited constructor! } FReadOnly := True; { set the default value } end;
C++:
//header file class PACKAGE TDBCalendar : public TSampleCalendar { private: bool FReadOnly; // field for internal storage protected: public: virtual __fastcall TDBCalendar(TComponent* Owner); __published: __property ReadOnly = {read=FReadOnly, write=FReadOnly, default=true}; }; //implementation file: virtual __fastcall TDBCalendar::TDBCalendar(TComponent* Owner) : TSampleCalendar(Owner) { FReadOnly = true; // sets the default value }
-
Override the SelectCell method to disallow selection if the control is read-only. Use of SelectCell is explained in Excluding Blank Cells.
Delphi:
function TDBCalendar.SelectCell(ACol, ARow: Longint): Boolean; begin if FReadOnly then Result := False { cannot select if read only } else Result := inherited SelectCell(ACol, ARow); { otherwise, use inherited method } end;
C++:
bool __fastcall TDBCalendar::SelectCell(long ACol, long ARow) { if (FReadOnly) return false; // can't select if read only return TSampleCalendar::SelectCell(ACol, ARow); // otherwise, use inherited method }
Remember to add the declaration of SelectCell to the type declaration of TDBCalendar, and append the override directive.
If you now add the calendar to a form, you will find that the component ignores clicks and keystrokes. It also fails to update the selection position when you change the date.