プロパティエディタ
標準の型でない特別な型のプロパティを定義するとき、プロパティエディタが必要になります。
プロパティエディタの2つのタイプ
プロパティエディタには2つのタイプがあります。1つはダイアログを開いてデータをユーザに編集をさせるタイプで、もうひとつは、オブジェクトインスペクタで値を文字列でユーザに編集させるタイプです。オブジェクトインスペクタはつねに値を文字列で編集するので、数値などを編集するときは、標準で用意されているこのタイプのプロパティエディタを使っていることになります。
オブジェクトインスペクタ型プロパティエディタの作成
(注意1)
TPropertyEditorから直接、派生せずにTPropertyEditorの派生クラスであるTIntegerPropertyやTFloatPropertyなどから派生させることもできます。
(注意2)
「コンポーネント/パッケージのインストール」でダイアログを開き、「設計時パッケージ」を選んで「編集」ボタンを押します。表示されたパッケージ編集ダイアログで「コンパイル」を実行します。
例
unit MyPropEdit; interface uses Windows, Messages, SysUtils, Classes, DsgnIntf; type TMyPropEditor = class(TIntegerProperty) public function GetValue: string; override; procedure SetValue(const Value: string); override; protected function GetAttributes: TPropertyAttributes; virtual; end; procedure Register; implementation uses MyComponent; function TMyPropEditor.GetValue: string; var lValue : LongInt; begin lValue := GetOrdValue; case lValue of 1: Result := 'FD1'; 2: Result := 'FD2'; 3: Result := 'Disk'; 4: Result := 'CD-ROM'; 5: Result := 'Network'; else Result := 'No Devices' end; end; procedure TMyPropEditor.SetValue(const Value: string); var lValue: LongInt; begin if Value = 'FD1' then lValue := 1 else if Value = 'FD2' then lValue := 2 else if Value = 'Disk' then lValue := 3 else if Value = 'CD-ROM' then lValue := 4 else if Value = 'Network' then lValue := 5 else raise EPropertyError.Create('Out of Range'); SetOrdValue(lValue); end; function TMyPropEditor.GetAttributes: TPropertyAttributes; begin Result := [paValueList, paReadOnly]; end; procedure Register; begin RegisterPropertyEditor(TypeInfo(TMyDevice), nil, '', TMyPropEditor); end; end;
ダイアログ型プロパティエディタの作成
ダイアログ例
type TLangPropEdit = class(TForm) Label1: TLabel; txtLangName: TEdit; Label2: TLabel; txtVersion: TEdit; cmdOK: TButton; procedure cmdOKClick(Sender: TObject); procedure FormActivate(Sender: TObject); private { Private 宣言 } FLang: 0..2; public { Public 宣言 } end; implementation {$R *.DFM} procedure TLangPropEdit.cmdOKClick(Sender: TObject); begin FLang := 1; Close end; procedure TLangPropEdit.FormActivate(Sender: TObject); begin txtLangName.Text := 'Delphi'; end;
TPropertyEditor派生クラス例
type TMyLangEditor = class(TIntegerProperty) public procedure Edit; override; function GetAttributes: TPropertyAttributes; virtual; end; var LangPropEdit: TLangPropEdit; procedure Register; implementation {$R *.DFM} procedure Register; begin RegisterPropertyEditor(TypeInfo(TMyLang), nil, '', TMyLangEditor); end; procedure TMyLangEditor.Edit; var Lang: TMyLang; LangEditor: TLangPropEdit; begin Lang := GetOrdValue; LangEditor := TLangPropEdit.Create(Application); try LangEditor.FLang := Lang; LangEditor.ShowModal; finally LangEditor.Free; end; end; function TMyLangEditor.GetAttributes: TPropertyAttributes; begin Result := [paDialog, paSubProperties]; end; end;