中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>控件开发>控件使用>文章内容

delphi 如何保存属性到运行期使用(上)

来源:国外 关于:Yoganand Aiyadurai 发布时间:2007-07-21   [收藏] [推荐]
Most of use a table or some kind of files to store the data for the application to pick up the data during run time. Actually we can store the data in the form file ( dfm ). In the following example I have created a component derived from the TPersistent class. It uses the TReader and TWriter class to Read and write to the respective streams. The TComponentEditor allows to define the design time editors to work with the component class. The TPropertyEditor class allows to define a property editor for a specialized property in a component class.
In the following example I have given the component's source code. The design time property editor has a source file code(pas) and source form code for the form (dfm). copy the dfm code to create a dfm file, name it as "propdlg.dfm" and assign it's Name property to "fmpropdlg" and the source file code to create a pas file, name it as "propdlg.pas". Install the component TMyComponent, include the file "propdlg.pas" of the property editor in the the package.
The component will then allow you to invoke the design time editor by clicking on the object inspector for the specified property or by right clicking on the component itself and then selecting the respective verb in the menu context. You can store the fields of the class Tmydata in the form file ( dfm ) during design time.
//**********************************************************************
//***** Component source (pas) *****************************************
//**********************************************************************
unit Test;
interface
uses
  Windows, Forms, Classes, StdCtrls, SysUtils, ComCtrls, Messages, Controls,
  {DB, DBCtrls, CommCtrl, OCIH, OCI, OCL, ExtVCs,} dsgnintf;
type
  TMyPropertyEditor = class(TPropertyEditor)
  private
    { Private declarations }
  public
    { Public declarations }
    function GetAttributes: TPropertyAttributes; override;
    procedure Edit; override;
    function GetValue: string; override;
  end;
  TMyEditorPopup = class(TComponentEditor)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure Edit; override;
    procedure ExecuteVerb(Index: Integer); override;
    function GetVerb(Index: Integer): string; override;
    function GetVerbCount: Integer; override;
  end;
  TMyData = class
  private
    Fstr: string;
    FInt: Integer;
  public
    property StringValue: string read Fstr write FStr;
    property IntegerValue: Integer read FInt write FInt;
  end;
  TMyTable = class(TPersistent)
  private
    FList: TList;
    function GetCount: Integer;
    function GetItem(Index: Integer): TMyData;
    procedure SetItem(Index: Integer; vItem: TMyData);
    procedure ReadProperties(Reader: TReader);
    procedure WriteProperties(Writer: TWriter);
  protected
    procedure DefineProperties(Filer: TFiler); override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure AddItem;
    procedure DeleteItem(Index: Integer);
    property ItemCount: Integer read GetCount;
    property Items[Index: Integer]: TMyData read GetItem write SetItem; default;
  end;
  TMyComponent = class(TComponent)
  private
    FMyTable: TMyTable;
    procedure SetTables(Value: TMyTable);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property MyTables: TMyTable read FMyTable write SetTables;
  end;
procedure Register;
implementation
uses PropDlg;
{ TMyTable }
constructor TMyTable.Create;
begin
  FList := TList.Create;
  FList.Clear;
end;
destructor TMyTable.Destroy;
begin
  FList.Free;
  FList := nil;
  inherited destroy;
end;
procedure TMyTable.DefineProperties(Filer: TFiler);
begin
  Filer.DefineProperty('Tables', ReadProperties, WriteProperties, True);
end;
procedure TMyTable.ReadProperties(Reader: TReader);
begin
  Reader.ReadListBegin;
  while (not Reader.EndOfList) do
  begin
    AddItem;
    with Items[itemCount - 1] do
    begin
      Fstr := Reader.ReadString;
      FInt := Reader.ReadInteger;
    end;
  end;
  Reader.ReadListEnd;
end;
procedure TMyTable.WriteProperties(Writer: TWriter);
var
  I: Integer;
begin
  Writer.WriteListBegin;
  for I := 0 to (ItemCount - 1) do
  begin
    with Items[I] do
    begin
      Writer.WriteString(Fstr);
      Writer.WriteInteger(FInt);
    end;
  end;
  Writer.WriteListEnd;
end;
procedure TMyTable.AddItem;
var
  vData: TMyData;
begin
  vData := TMyData.Create;
  FList.Add(vData);
end;
function TMyTable.GetCount: Integer;
begin
  Result := FList.Count;
end;
function TMyTable.GetItem(Index: Integer): TMyData;
begin
  Result := TMyData(FList[Index]);
end;
procedure TMyTable.SetItem(Index: Integer; vItem: TMyData);
begin
  Flist[Index] := vItem;
end;
procedure TMyTable.DeleteItem(Index: Integer);
begin
  FList.Delete(Index);
end;

[浏览: 次]   
上一篇:delphi 如何将D5组件升到D6   下一篇:delphi 如何将属性在设计期保存到DFM文件(中)
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi treeview 使用
·delphi delphi下对象类别检查
·delphi 组件序列化
·delphi 如何在DBGRID里添加行序号
·delphi 组件用户自定义设计期工具
·delphi 显示GRID单Cell框的Hint
·delphi 组件序列化
·delphi 如何将系统所有菜单列到树上Tre
·delphi 如何保存属性到运行期使用(下)
·delphi 如何将属性在设计期保存到DFM文
·delphi 如何保存Blob类型字段到数据库
·delphi VCL控件机制与VCL.net控件机制
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932