S
ssg
Unregistered / Unconfirmed
GUEST, unregistred user!
程序编制中发现一个奇怪的现象源代码如下)
其中,Form1为主窗口,Form2为动态生成的子窗口,DataModual为数据模块;
DataModual1上置一TDatabase平构件,程序运行时自动连接数据服务器(用的是DB2);
Form1上有主菜单File,按New时动态生成Form2;
Form2上有Table1,连接置数据库的一个表,另有Dbgrid和dbnavigator,窗口生成
时打开表,关闭时关闭表;
通过试验发现,当Form2的属性为fsdialog时,程序运行正常(重复打开关闭对话框),而Form2的属性为fsMdiChild时,即使保证只打开一个子窗口(打开后又关闭),在下次打开子窗口时出现"Open语句中指定的游标已经打开"错误.
这是为什么????Form2都是动态生成,为什么结果却不同呢???
请各位高手赐教.
主窗口:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
file1: TMenuItem;
new1: TMenuItem;
procedure new1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.new1Click(Sender: TObject);
var
child: TForm2;
begin
Child:= TForm2.Create( application );
{ try
Child.Showmodal;
finally
Child.free;
end; }
end;
end.
子窗口:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, DBTables;
type
TForm2 = class(TForm)
DataSource1: TDataSource;
Table1: TTable;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses unit3;
{$R *.DFM}
procedure TForm2.FormShow(Sender: TObject);
begin
table1.open;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
table1.close;
// action:= cafree;
end;
end.
数据模块:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
type
TDataModule3 = class(TDataModule)
db1: TDatabase;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule3: TDataModule3;
implementation
{$R *.DFM}
procedure TDataModule3.DataModuleCreate(Sender: TObject);
begin
db1.open;
end;
procedure TDataModule3.DataModuleDestroy(Sender: TObject);
begin
db1.close;
end;
end.
其中,Form1为主窗口,Form2为动态生成的子窗口,DataModual为数据模块;
DataModual1上置一TDatabase平构件,程序运行时自动连接数据服务器(用的是DB2);
Form1上有主菜单File,按New时动态生成Form2;
Form2上有Table1,连接置数据库的一个表,另有Dbgrid和dbnavigator,窗口生成
时打开表,关闭时关闭表;
通过试验发现,当Form2的属性为fsdialog时,程序运行正常(重复打开关闭对话框),而Form2的属性为fsMdiChild时,即使保证只打开一个子窗口(打开后又关闭),在下次打开子窗口时出现"Open语句中指定的游标已经打开"错误.
这是为什么????Form2都是动态生成,为什么结果却不同呢???
请各位高手赐教.
主窗口:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
file1: TMenuItem;
new1: TMenuItem;
procedure new1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.DFM}
procedure TForm1.new1Click(Sender: TObject);
var
child: TForm2;
begin
Child:= TForm2.Create( application );
{ try
Child.Showmodal;
finally
Child.free;
end; }
end;
end.
子窗口:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, Db, DBTables;
type
TForm2 = class(TForm)
DataSource1: TDataSource;
Table1: TTable;
DBGrid1: TDBGrid;
DBNavigator1: TDBNavigator;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses unit3;
{$R *.DFM}
procedure TForm2.FormShow(Sender: TObject);
begin
table1.open;
end;
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
table1.close;
// action:= cafree;
end;
end.
数据模块:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Db, DBTables;
type
TDataModule3 = class(TDataModule)
db1: TDatabase;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
DataModule3: TDataModule3;
implementation
{$R *.DFM}
procedure TDataModule3.DataModuleCreate(Sender: TObject);
begin
db1.open;
end;
procedure TDataModule3.DataModuleDestroy(Sender: TObject);
begin
db1.close;
end;
end.