delphi.gif (306 バイト) プロパティエディタ


標準の型でない特別な型のプロパティを定義するとき、プロパティエディタが必要になります。

 

toach.gif (917 バイト) プロパティエディタの2つのタイプ

プロパティエディタには2つのタイプがあります。1つはダイアログを開いてデータをユーザに編集をさせるタイプで、もうひとつは、オブジェクトインスペクタで値を文字列でユーザに編集させるタイプです。オブジェクトインスペクタはつねに値を文字列で編集するので、数値などを編集するときは、標準で用意されているこのタイプのプロパティエディタを使っていることになります。

 

toach.gif (917 バイト) オブジェクトインスペクタ型プロパティエディタの作成

  1. 「新規作成/ユニット」を実行して空のユニットを作成します。
  2. そのユニットにTPropertyEditor派生クラスをエディタで追加(記述)します。(注意1)
  3. GetValue,SetValue,GetAttributesをオーバーライドします。
  4. そのユニットにRegister手続きをエディタで追加(記述)します。
  5. 対応するコンポーネントと同じパッケージにコンパイル/インストールします。(注意2)

(注意1)

TPropertyEditorから直接、派生せずにTPropertyEditorの派生クラスであるTIntegerPropertyやTFloatPropertyなどから派生させることもできます。

(注意2)

「コンポーネント/パッケージのインストール」でダイアログを開き、「設計時パッケージ」を選んで「編集」ボタンを押します。表示されたパッケージ編集ダイアログで「コンパイル」を実行します。

delphi1.gif (322 バイト)

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;

 

toach.gif (917 バイト) ダイアログ型プロパティエディタの作成

  1. 「新規作成/ユニット」を実行してフォームを作成します。
  2. そのフォームにコントロールを貼り付けて、プロパティ編集ダイアログを作成します。
  3. ダイアログが表示されたときのコントロールの状態を決めるコードを記述します。
  4. ダイアログでOKボタンが押されたときの、プロパティの値を決めるコードを記述します。
  5. そのユニットにTPropertyEditor派生クラスをエディタで追加(記述)します。(前述の注意1参照)
  6. Edit,GetAttributesをオーバーライドします。
  7. そのユニットにRegister手続きをエディタで追加(記述)します。
  8. 対応するコンポーネントと同じパッケージにコンパイル/インストールします。(前述の注意2参照)

delphi1.gif (322 バイト)ダイアログ例

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;

delphi1.gif (322 バイト)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;