Resource not found error when using TForm as base for a component

六月ゝ 毕业季﹏ 提交于 2020-01-09 08:11:50

问题


I am writing a component and want to change the base type to a TForm however at run time I get the error "Resource TMyComp not found". I guess that this is because there is no dfm but I am not sure what to do about it.

Thanks

unit Unit65;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TMyComp = class(TForm);

  TForm65 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    Mc: TMyComp;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form65: TForm65;

implementation

{$R *.dfm}

procedure TForm65.Button1Click(Sender: TObject);
begin
  Mc := TMyComp.Create(Self);
  Mc.Parent := nil;
  Mc.ShowModal;
end;

end.

回答1:


There is no .dfm file for TMyComp. You can avoid attempting to load the .dfm by calling the CreateNew constructor rather than Create.

Mc := TMyComp.CreateNew(Self);

From the documentation:

Use CreateNew instead of Create to create a form without using the associated .DFM file to initialize it. Always use CreateNew if the TCustomForm descendant is not a TForm object or a descendant of TForm.

CreateNew bypasses the streaming in of the previously-associated .DFM file. If the form contains visual components, therefore, you must stream in an external .DFM to bind the visual components with their classes. If the newly created form has an external .DFM file, then you can follow the call to CreateNew with a call to InitInheritedComponent. If you need to create the .dfm file for the new form instance, bracket the call to CreateNew with calls to WriteComponentResFile and ReadComponentResFile.



来源:https://stackoverflow.com/questions/41889732/resource-not-found-error-when-using-tform-as-base-for-a-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!