Delphi Tips 
-----------------------------

0346  D1   D2   D3   D4   D5   D6   D7   3.1   95   98    作成: 2003/08/22 osamu rev 1.1
   B1   B3   B4   B5   B6   B7   NT3   NT4   2K   XP  更新: 2003/08/22 osamu 編集
プロパティ値を文字列に変換/逆変換

published なプロパティを Ini に書き込みたい・から読み出したいというような時、プロパティ値を文字列に変換する必要が生じます。

Font プロパティを Ini/Registry に保存したい という投稿はFAQに近いですが、この記事はそういった要望への回答となります。

Enum/Set だけではなく、TColor のように「Enum ではないけど文字で書きこまれることがあるプロパティ」にも対応しています。

uses Classes, TypInfo;

function PropertyToString(obj: TPersistent; PropName: string): string;
var IntToIdent: TIntToIdent;
    Ident: string;
begin
  Result:= GetPropValue(obj, PropName);
  if PropType(obj, PropName)=tkInteger then begin
    IntToIdent := FindIntToIdent(GetPropInfo(obj, PropName).PropType^);
    if Assigned(IntToIdent) then
      IntToIdent(GetPropValue(obj, PropName), Result);
  end;
end;

procedure StringToProperty(s: string; obj: TPersistent; PropName);
var IdentToInt: TIdentToInt;
    i: Integer;
begin
  if PropType(obj, PropName)==tkInteger then begin
    IdentToInt:= FindIdentToInt(GetPropInfo(obj, PropName).PropType^);
    if Assigned(IdentToInt) and IdentToInt(s, i) then begin
      SetPropValue(obj, PropName, i);
    end else begin
      SetPropValue(obj, PropName, s);
    end;
  end else begin
    SetPropValue(obj, PropName, s);
  end;
end;
参照: [Delphi-ML:75958] <文字列> <PASCAL>

[新規作成] [最新の情報に更新]

How To
Lounge
KeyWords


Tips
Delphi
Home
Osamu Takeuchi osamu@big.or.jp