1

我正在开发一个自定义组件,Delphi -7其中我有一些published属性

   private
     { Private declarations }
     FFolderzip  ,Fimagezip,Ftextzip,FActivatezipComp   : Boolean;
     FMessagebo : string;
   published
    { Published declarations }
    {component Properties}
    {#1.Folder Zip}
    property ZipFolder : Boolean read FFolderzip  write FFolderzip  default False;
    {#2.Send imagezip ?}
    property ZipImage : Boolean read Fimagezip   write Fimagezip   default False;
    {#3.text files}
    property ZipText : Boolean read Ftextzip   write Ftextzip   default False;
    {#4.message}
    property ZipMessage: String read FMessagebo write FMessagebo ; 
    {#5.Activate }
    property ActivateZipper: Boolean read FActivatezipComp write  FActivatezipComp Default false;
   end;

当用户将组件放在应用程序上时,ActivateZipper属性会为用户提供一个选项来激活/启用或停用/禁用组件执行。该组件创建一个文件,所以在构造函数中我有这个,CreateATextFileProc将在应用程序文件夹中创建文件。所以如果我检查构造函数ActivateZipper是真还是假..

我有一个constructor

     constructor TcomProj.Create(aOwner: TComponent);
     begin
       inherited;
       if ActivateZipper then CreateATextFileProc;
     end;

ActivateZipper即使我在对象检查器中将其设置为 true,它也始终为 false 。如何禁止组件使用已发布的属性?

4

2 回答 2

4

构造函数为时过早。设计时属性值尚未流入组件。您需要等到组件的Loaded()方法被调用,然后才能对这些值进行操作。如果您在运行时动态创建组件,您还需要一个属性设置器,因为没有 DFM 值,因此Loaded()不会被调用。

type
  TcomProj = class(TComponent)
  private 
    ...
    procedure SetActivateZipper(Value: Boolean);
  protected
    procedure Loaded; override;
  published 
    property ActivateZipper: Boolean read FActivatezipComp write SetActivateZipper; 
  end; 

procedure TcomProj.SetActivateZipper(Value: Boolean);
begin
  if FActivatezipComp <> Value then
  begin
    FActivatezipComp := Value;
    if ActivateZipper and ((ComponentState * [csDesigning, csLoading, csLoading]) = []) then
      CreateATextFileProc; 
  end;
end;

procedure TcomProj.Loaded;
begin
  inherited;
  if ActivateZipper then CreateATextFileProc; 
end;
于 2012-06-29T16:33:38.737 回答
2

ActivateZipper即使我在对象检查器中将其设置为 True,它也始终为 False。

您的激活码现在放置在构造函数中。有几件事:

  • 在构造函数中,所有私有字段都被初始化为零(0、''、null、nil,取决于类型)。如果您没有另外设置,则一旦创建组件,这些字段将保持为零。
  • 当您使用对象检查器更改属性时,组件已经创建。
  • 你的代码永远不会运行。
  • 如果您确实想在创建时对其进行初始化,则还要更改默认存储说明符。

您需要的是一个属性设置器,只要通过对象检查器或代码更改属性,就会调用该设置器:

  private
    procedure SetZipperActive(Value: Boolean);
  published
    property ZipperActive: Boolean read FZipperActive write SetZipperActive default False;

procedure TcomProj.SetZipperActive(Value: Boolean);
begin
  if FZipperActive <> Value then
  begin
    FZipperActive := Value;
    if FZipperActive then
      CreateATextFile
    else
      ...
end;

您可能会考虑在设计时关闭此功能,因为您可能只希望在运行时进行实际压缩。然后测试csDesigning标志ComponentState

procedure TcomProj.SetZipperActive(Value: Boolean);
begin
  if FZipperActive <> Value then
  begin
    FZipperActive := Value;
    if csDesigning in ComponentState then
      if FZipperActive then
        CreateATextFile
      else
        ...
end;
于 2012-06-29T10:12:50.097 回答