delphi:如何将TCustomFrame和记录存储在一个列表中

Den*_* F. 3 delphi list record tframe

我正在使用一个TObjectList<TCustomFrame>商店TCustomFrames.现在我想TCustomFrame在同一个列表中存储更多有关的信息.一个record会很好.

德尔福类,你会更喜欢保存TCustomFrames,并records在同一个列表?

TCustomFramesrecords将在运行时添加.

Dav*_*nan 7

创建一条记录来保存所有信息:

type 
  TFrameInfo = record
    Frame: TCustomFrame;
    Foo: string;
    Bar: Integer;
  end;
Run Code Online (Sandbox Code Playgroud)

把它放在一个TList<TFrameInfo>.

我注意到你使用的是TObjectList<T>而不是TList<T>.唯一的好理由这样做,这将是如果你设置OwnsObjectsTrue.但这似乎不太可能,因为我怀疑列表是否真正负责GUI对象的生命周期.至于对未来的注意,如果您发现自己使用TObjectList<T>OwnsObjects一套来False,那么你不妨切换TList<T>.

现在,如果您确实需要列表来控制生命周期,那么您最好使用类而不是记录TFrameInfo.

type 
  TFrameInfo = class
  private
    FFrame: TCustomFrame;
    FFoo: string;
    FBar: Integer;
  public
    constructor Create(AFrame: TCustomFrame; AFoo: string; ABar: Integer);
    destructor Destroy; override;
    property Frame: TCustomFrame read FFrame;
    // etc.
  end;

  constructor TFrameInfo.Create(AFrame: TCustomFrame; AFoo: string; ABar: Integer);
  begin
    inherited Create;
    FFrame := AFrame;
    // etc.
  end;

  destructor TFrameInfo.Destroy; 
  begin
    FFrame.Free;
    inherited;
  end;
Run Code Online (Sandbox Code Playgroud)

然后在此举行TObjectList<TFrameInfo>具有OwnsObjects设置为True.