这个程序整整折磨了我将近3天的时间,在百度CSDN,以及各方面友人帮助,终于搞明白了,
程序会比较长先整理整理一些基本概念,这些delphi的基本概念之前我根本就不了解
概念1
问:如何将在窗体2中引用窗体1中的类?
答:在窗体2中的uses中添加窗体1的文件名!
概念2
问:如何在两个窗体中来回切换
答:在implementation下面的uses中写入窗口名
as用于组件类型转换
inherited调用父类的过程
菜单栏中的progect->options->forms可调整窗口的先后次序,以及是否屏蔽窗口的操作
每个组件创建时初始化都要执行create操作既为此组件分配内存
一下就是程序了 一共3个窗体
窗体1程序
unit CommonForm;//窗体一的名字
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMyForm = class(TForm)
protected
constructor Create(Aowner: TComponent); override;//override表示重载过程
end;
var
MyForm: TMyForm;//定义的tmyfrom类
implementation
{$R *.dfm}
constructor TMyForm.Create(Aowner: TComponent);
begin
inherited create(aowner);//执行父类的create过程,开辟内存空间
self.Caption := '123';//将窗体命名为123
end;
end.
窗体2
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
CommonForm, Dialogs, StdCtrls;//在窗体2中如果要引用窗体1中的myfrom类则必须再次加上窗体1的名字
type
TForm1 = class(TMyForm)//窗体1中定义的类myfrom
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses Unit2;//要想调用窗体3则将窗体3的名字加入此位置
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Form2.Show;//显示form2
Form1.Hide;//将form1关闭
end;
end.
窗体3
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, CommonForm, StdCtrls;
type
TForm2 = class(TMyForm)//引用窗体1中的类
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
Form2.Hide;
Form1.Show;
end;
end.
另外如果将窗体1改成如下也个可以达到预期的效果
unit CommonForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
Tmyname = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
myname: Tmyname;
implementation
{$R *.dfm}
procedure Tmyname.FormCreate(Sender: TObject);//窗体1的onchange事件
begin
Self.Caption:='123';
end;
end.
因为此窗体的onchange事件是公有的 窗体2,窗体3继承了myform也包括窗体1的onchange事件 所以也可以达到预期的效果