ReadOnly プロパティの追加
コントロールの読み取り専用化 への移動
ReadOnly プロパティを追加することにより、設計時にコントロールを読み取り専用にする手段を提供します。そのプロパティが True に設定されると、コントロール内のすべてのセルを選択不可にすることができます。
ReadOnly プロパティを追加するには:
- プロパティ宣言と、プロパティの値を格納するための private フィールドを追加します。
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 }
SelectCell メソッドをオーバーライドして、コントロールが読み取り専用の場合には選択不可にします。SelectCell の使用については、「空白セルの除外」を参照してください。
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 }
必ず、TDBCalendar の型宣言に SelectCell の宣言を追加し、override 指令を付け加えます。
カレンダーをフォームに追加すれば、コンポーネントでクリックやキー入力が無視されるのがわかります。日付を変更しても、選択位置は更新されません。