1

Имеется самописный (в процессе) класс:

  TCardGridImage = class
    FCardPanel: TCardPanel;
    procedure SetCardPanel (Source: TCardPanel);
  public
    property CardPanel: TCardPanel write SetCardPanel; //FCardPanel;
  end;

procedure TCardGridImage.SetCardPanel (Source: TCardPanel);
var
  ms: TMemoryStream;
  OldName: string;
  as1, as2: Boolean;
begin
  as1 := Assigned(Source);
  as2 := Assigned(FCardPanel);
  OldName := Source.Name;
  Source.Name := ''; // needed to avoid Name collision
  try
    ms := TMemoryStream.Create;
    try
      ms.WriteComponent(Source);
      ms.Position := 0;
      ms.ReadComponent(FCardPanel);
    finally
      ms.Free;
    end;
  finally
    Source.Name := OldName;
  end;
end;

Пытаюсь присвоить в FCardPanel CardPanel1, но на строке ms.ReadComponent(FCardPanel); выпадает ошибка:

raised exception class EClassNotFound with message 'Class TCardPanel not found'

Как обойти или исправить?

3
  • 1
    Что такое TCardPanel? Оно наследник TPersistent? RegisterClass для него выполнено?
    – MBo
    17 окт 2022 в 15:00
  • TCardPanel = class(TCustomCardPanel) находится в Vcl.WinXPanels
    – woojin
    17 окт 2022 в 15:22
  • А не проще в свой собственный компонент встроить какой-нибудь метод function Duplicate(): TCardPanel; который будет сам создавать свою правильную копию?
    – Alekcvp
    18 окт 2022 в 10:19

1 ответ 1

0

ОК, значит наследник TPersistent.

Класс должен быть зарегистрирован. Для компонентов в published-области формы это выполняется автоматически, если я правильно помню, а в данном случае нужно сделать то, что я написал выше - RegisterClass

Put the call to RegisterClass in a Register procedure. In Delphi, you can also put the call in the initialization section of the unit in which the class is defined.

Unregistered classes can't be loaded or saved by the component streaming system

3
  • сделал так initialization RegisterClass(TCardPanel); ошибки при ms.ReadComponent(FCardPanel); больше нет, но переменная содержит nil
    – woojin
    17 окт 2022 в 15:47
  • @woojin создайте объект FCardPanel := T.Crreate(self) а после readComponent или function TStream.ReadComponent(const Instance: TComponent): TComponent; парамент const а не var а вот result там ваш компонент
    – vegat4
    19 окт 2022 в 17:31
  • @vrgat4 попробую
    – woojin
    19 окт 2022 в 18:41

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.