问题出在那儿?(关于类之间调用的问题)(200分)

  • 主题发起人 主题发起人 SamHunt
  • 开始时间 开始时间
S

SamHunt

Unregistered / Unconfirmed
GUEST, unregistred user!
我建立了三个窗体,
form1是mid主框,
form2是mid子框,
form3是普通的窗体,
form1 建立 form2 ,form2动态建立form3,
问题是这样的:当form2调用form3时,正常,
但当form3操作form2时,就出现非法操作!!
问题出在那儿?

program Project1;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};
{$R *.RES}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
// Application.CreateForm(TForm3, Form3);
Application.Run;
end.


unit Unit1;
interface
uses
Windows, Messages, SysUtils, 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);
TForm2.Create(application);
// Form2.ShowModal;
end;

end.


unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, ADODB, Grids, DBGrids, StdCtrls;
type
TForm2 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADOTable1: TADOTable;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form2: TForm2;
implementation
uses Unit3;
{$R *.DFM}
procedure TForm2.Button1Click(Sender: TObject);
var
aa:TForm3;
begin
aa:=TForm3.Create(Application);
aa.ShowModal;
end;

end.


unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

//var
// Form3: TForm3;
implementation
uses Unit2;
{$R *.DFM}
procedure TForm3.Button1Click(Sender: TObject);
begin
Unit2.Form2.ADOTable1.Next;
end;

end.
 
试试用下面的代码看看。工程文件和三个单元文件如下,主要是更改了MDI窗体的Button按钮
的代码,引用Unit2中的一个过程ShowUnit2,不要忘了在Unit的Close中添加“Form2:=nil,Action:=caFree“的
代码,否则关闭Form2将出问题。
program Project2;
uses
Forms,
Unit1 in 'Unit1.pas' {Form1},
Unit2 in 'Unit2.pas' {Form2},
Unit3 in 'Unit3.pas' {Form3};
{$R *.res}
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

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
ShowUnit2;
end;

end.

unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure ShowUnit2;
var
Form2: TForm2;

implementation
uses Unit3;
{$R *.dfm}
procedure ShowUnit2;
begin
Form2:=TForm2.Create(Application);
if not Assigned(Form2) then
begin
Form2.Show;
end;
end;

procedure TForm2.Button1Click(Sender: TObject);
var
aa:TForm3;
begin
aa:=TForm3.Create(Application);
aa.ShowModal;
end;

end.

unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form3: TForm3;
implementation
uses Unit2;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
Form2.Edit1.Text:='Hello';
//这里可以换成你的 Form2.ADOTable1.Next;
end;

end.
 
用form2.show;不用ShowModal;
 
两处修改:
1、
aa:=TForm3.Create(Application);
改成
aa:=TForm3.Create(self);
2、
form3中访问form2时不要直接使用“form2”
而是用: TForm2(Owner)
比如:不用 Unit2.Form2.ADOTable1.Next
而用:TForm2(Owner).ADOTable1.Next
 
建議將form1的button1的onclick事件改為
procedure TForm1.Button1Click(Sender: TObject);
begin
form2:=TForm2.Create(application);
form2.show;
end;
 

Similar threads

后退
顶部