使用TForm作为组件的基础时找不到资源错误 [英] Resource not found error when using TForm as base for a component

查看:112
本文介绍了使用TForm作为组件的基础时找不到资源错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个组件,并希望将基本类型更改为TForm,但在运行时我收到错误Resource TMyComp not found。我想这是因为没有dfm,但我不知道该怎么做。



谢谢



<单位Unit65; pre>

接口

使用
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;
{私人声明}
public
{公开声明}
end;

var
Form65:TForm65;

执行

{$ R * .dfm}

程序TForm65.Button1Click(发件人:TObject);
begin
Mc:= TMyComp.Create(Self);
Mc.Parent:= nil;
Mc.ShowModal;
结束

结束。


解决方案

没有.dfm文件为 TMyComp 。您可以通过调用 <$ c $来尝试加载.dfm c> CreateNew 构造函数,而不是创建

  Mc:= TMyComp.CreateNew(Self); 

文档


使用CreateNew而不是Create创建一个表单,而不使用
相关联的.DFM文件来初始化它。如果
TCustomForm后代不是TForm对象或TForm的后代,则始终使用CreateNew。



CreateNew绕过之前关联的.DFM
文件。因此,如果表单包含可视化组件,则必须在外部.DFM中
流以将视觉组件与
类绑定。如果新创建的表单有一个外部的.DFM文件,那么
可以跟着调用CreateNew调用
InitInheritedComponent。如果您需要为
新表单实例创建.dfm文件,则将调用括起来,调用
WriteComponentResFile和ReadComponentResFile。



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.

解决方案

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.

这篇关于使用TForm作为组件的基础时找不到资源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆