This question already has an answer here:
Resource not found error when using TForm as base for a component

(1个答案)


3年前关闭。





我将这些代码放在一起以创建动态表单

unit Unit1;

 interface

 uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

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


  type
    TForm2 = class(TForm)
     private
       { Private declarations }
    public
      { Public declarations }
    end;

  var
    Form2: TForm2;
    Form1: TForm1;

implementation

  {$R *.dfm}

  procedure TForm1.Button1Click(Sender: TObject);
 var
     a:TForm2;
 begin
     a:=TForm2.Create(nil);
   end;


结束。

我收到一条错误消息,指出找不到资源tform2。我必须做什么?

谢谢

最佳答案

您正在调用TForm.Create()构造函数,该构造函数从DFM加载TForm内容,但是您的项目没有用于TForm2的DFM,这就是为什么您遇到资源错误的原因。要跳过这一点,您需要使用TForm.CreateNew()构造函数。

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TForm2;
begin
  a := TForm2.CreateNew(nil, 0);
  ...
end;

10-07 18:26