禁用/取消激活具有已发布属性的自定义组件

时间:2012-06-29 09:41:35

标签: delphi constructor delphi-7 custom-component

我正在使用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;

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

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

  即使我在对象检查器中将其设置为True,

ActivateZipper也总是为假。

您的激活码现在放在构造函数中。关于这一点的一些事情:

  • 在构造函数中,所有私有字段都是零(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;

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

procedure TcomProj.SetZipperActive(Value: Boolean);
begin
  if FZipperActive <> Value then
  begin
    FZipperActive := Value;
    if csDesigning in ComponentState then
      if FZipperActive then
        CreateATextFile
      else
        ...
end;
相关问题