我如何从内存中销毁(释放)表单? [英] How i can to Destroy (free) a Form from memory?

查看:154
本文介绍了我如何从内存中销毁(释放)表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中有2个Form(Form1和Form2),Form1是自动创建表单,但是Form2是Available表单。
我如何创建Form2并卸载Form1?

i have 2 Form (Form1 and Form2) in the my project, Form1 is Auto-create forms, but Form2 is Available forms. how i can to create Form2 and unload Form1?

此代码中出现访问验证错误。

I received a "Access validation" Error in this code.

这是Form1代码:

1.  uses Unit2;
//*********
2.  procedure TForm1.FormCreate(Sender: TObject);
3.  var a:TForm2;
4.  begin
5.      a := TForm2.Create(self);
6.      a.Show;
7.      self.free;  // Or self.destory;
8.  end;

谢谢。

我将 Serg代码修改为:

I modified that "Serg" code to this :

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;
var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Application.CreateForm(TForm2, Form2);
  Release;
end;

end.

///

program Project1;
uses
  Forms,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Form1:= TForm1.Create(Application);
  Application.Run;
end.

但是此项目开始然后自动退出,为什么?
我想显示Form1,然后单击Button1,然后显示Form2和free(Release)Form1。我该怎么办?

but this project start and then exit automatically, Why? i want to show Form1 and when we click Button1 then show Form2 and free(Release) Form1. how i can to this?

推荐答案

销毁表单时,最好使用Release。

When you destroy a form, it's better to use Release.

释放与免费几乎相同,但是它等待挂起的消息以避免崩溃。

Release is almost the same as free, but it waits for pending messages to avoid crashes.

您永远不要使用Destroy。 Free / Release调用析构函数。

You should never use Destroy. Free/Release calls the destructor.

Self是当前对象(在您的代码Form1中,因此self.Free会杀死当前表单。这会导致访问冲突。Form1

Self is the current object (in your code Form1, so self.Free kills the current form. Which results in the access violation. Form1 is auto created, it is also auto destroyed so you shouldn't destroy it yourself. If you don't want it, hide it.

并且应该保留一个引用,它应该是自动创建的,也可以自动销毁,所以您不应该自己销毁它。

And you should keep a reference to the newly created form if you want to handle it later.

您修改后的代码应类似于:

Your modified code should be like:

uses Unit2;

TForm1 = class (TForm)
private
  FChild : TForm2;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FChild := TForm2.Create(nil);
  Hide; // Hides form 1
  FChild.Show;
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  FChild.Release;
end;

但是为什么要在第一个表单的表单创建中创建另一个表单,为什么不完全删除第一个表单并只使用第二个(自动创建的)?

But Why do you want to create another form in the form create of the first form. Why not remove the first form entirely and only use the second one (auto created)?

这篇关于我如何从内存中销毁(释放)表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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