开发者

not able to create a dynamic form in Delphi 7 [duplicate]

This question already has an answer here: Resource not found error when using TForm as base for a component (1 answer) Closed 6 years ago.

I have put togethe开发者_JAVA百科r this code for creating a dynamic form

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;

end.

I get an error saying resource tform2 cannot be found. What must i do?

Thanks


You are calling the TForm.Create() constructor that loads the TForm contents from a DFM, but your project does not have a DFM for TForm2, which is why you are getting the resource error. To skip that, you need to use the TForm.CreateNew() constructor instead.

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


In Delphi you must declare only one form per unit, also each form needs a dfm file, that file store the form definition and components properties. In your code you have this error because the application can't found the dfm file for the TForm2 form. So to fix the problem just create a new form (TForm2) in a separate unit and then add the unit a name to the unit where you need to call the TForm2.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜