ReadOnly プロパティの追加

提供: RAD Studio
移動先: 案内検索

コントロールの読み取り専用化 への移動


ReadOnly プロパティを追加することにより、設計時にコントロールを読み取り専用にする手段を提供します。そのプロパティが True に設定されると、コントロール内のすべてのセルを選択不可にすることができます。

ReadOnly プロパティを追加するには:

  1. プロパティ宣言と、プロパティの値を格納するための private フィールドを追加します。
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; 
//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
}
  1. SelectCell メソッドをオーバーライドして、コントロールが読み取り専用の場合には選択不可にします。SelectCell の使用については、「空白セルの除外」を参照してください。

    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; 
    
    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 指令を付け加えます。

カレンダーをフォームに追加すれば、コンポーネントでクリックやキー入力が無視されるのがわかります。日付を変更しても、選択位置は更新されません。

関連項目