Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

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.
like image 303
Peter Avatar asked Jan 27 '17 08:01

Peter


1 Answers

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.

like image 72
David Heffernan Avatar answered Oct 24 '22 08:10

David Heffernan