B
bytelife
Unregistered / Unconfirmed
GUEST, unregistred user!
程序如下
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Panel1: TPanel;
procedure closeallform;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2,unit3;
{$R *.dfm}
procedure TForm1.closeallform;
begin
if Assigned(Form2) then
begin
form2.Close;
Form2.Free;
form2 := nil;
end;
if Assigned(Form3) then
begin
form3.Close;
Form3.Free;
form3 := nil;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
closeallform;
if not Assigned(form2) then
begin
Form2 := TForm2.Create(Application);
form2.Parent:=panel1;
form2.Show;
form2.Align:=alclient;
end;
// form2.ShowModal;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
closeallform;
if not Assigned(form3) then
begin
Form3 := TForm3.Create(Application);
form3.Parent:=panel1;
form3.Show;
form3.Align:=alclient;
end;
end;
end.
以上是主窗口代码,大意是把FORM2、FORM3显示在主窗口的PANEL1上,每次打开一个FORM前检查PANEL1上有无FORM打开,有则关闭。
FORM3上有一QRPREVIEW1,用来实现报表的自定义预览。
FORM3的大致代码:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, QRPrntr, ExtCtrls, DB, ADODB;
type
TForm3 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
ADOTable1: TADOTable;
QRPreview1: TQRPreview;
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses unit4;
{$R *.dfm}
procedure TForm3.Button8Click(Sender: TObject);
begin
adotable1.Close;
close;
end;
procedure TForm3.Button9Click(Sender: TObject);
begin
adotable1.Open;
quickreport4.Preview;
end;
procedure TForm3.Button3Click(Sender: TObject);
begin
qrpreview1.Zoom:=100;
end;
end.
quickreport4中的 ONpreview 事件代码:
procedure TQuickReport4.QuickRepPreview(Sender: TObject);
begin
form3.QRPreview1.QRPrinter :=quickreport4.QRPrinter;
end;
整个过程是这样的,主窗口BUTTON2调用FORM3,在FORM1的PANEL1上能正常显示FORM3,
FORM3中TForm3.Button9Click也能在FORM3的QRPREVIEW1上正常预览报表,
问题出在如果我退出FORM3后再调用FORM1的CLOSEALLFORM就会提示出错。
错误提示是:
“Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00403988 in module 'PROJECT1.EXE'. Read of address 00C7C000'. Process stopped. Use Step or Run to continue.”
查了下程序,问题出在CLOSEALLFORM中的:
if Assigned(Form3) then
begin
form3.Close;
Form3.Free;
form3 := nil;
end;
也就是说在释放FORM3时程序会出错,
搞不懂是怎么会事?是不是因为这时候QUICKREPORT还没释放?
如果在FORM3中没有对QUICKREPORT调用则不会出现这样的问题。
另,在PROJECT OPTION中FORM2、FORM3都不是自动创建的。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Panel1: TPanel;
procedure closeallform;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2,unit3;
{$R *.dfm}
procedure TForm1.closeallform;
begin
if Assigned(Form2) then
begin
form2.Close;
Form2.Free;
form2 := nil;
end;
if Assigned(Form3) then
begin
form3.Close;
Form3.Free;
form3 := nil;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
closeallform;
if not Assigned(form2) then
begin
Form2 := TForm2.Create(Application);
form2.Parent:=panel1;
form2.Show;
form2.Align:=alclient;
end;
// form2.ShowModal;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
closeallform;
if not Assigned(form3) then
begin
Form3 := TForm3.Create(Application);
form3.Parent:=panel1;
form3.Show;
form3.Align:=alclient;
end;
end;
end.
以上是主窗口代码,大意是把FORM2、FORM3显示在主窗口的PANEL1上,每次打开一个FORM前检查PANEL1上有无FORM打开,有则关闭。
FORM3上有一QRPREVIEW1,用来实现报表的自定义预览。
FORM3的大致代码:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, QRPrntr, ExtCtrls, DB, ADODB;
type
TForm3 = class(TForm)
Panel1: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Button5: TButton;
Button6: TButton;
Button7: TButton;
Button8: TButton;
Button9: TButton;
ADOTable1: TADOTable;
QRPreview1: TQRPreview;
procedure Button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses unit4;
{$R *.dfm}
procedure TForm3.Button8Click(Sender: TObject);
begin
adotable1.Close;
close;
end;
procedure TForm3.Button9Click(Sender: TObject);
begin
adotable1.Open;
quickreport4.Preview;
end;
procedure TForm3.Button3Click(Sender: TObject);
begin
qrpreview1.Zoom:=100;
end;
end.
quickreport4中的 ONpreview 事件代码:
procedure TQuickReport4.QuickRepPreview(Sender: TObject);
begin
form3.QRPreview1.QRPrinter :=quickreport4.QRPrinter;
end;
整个过程是这样的,主窗口BUTTON2调用FORM3,在FORM1的PANEL1上能正常显示FORM3,
FORM3中TForm3.Button9Click也能在FORM3的QRPREVIEW1上正常预览报表,
问题出在如果我退出FORM3后再调用FORM1的CLOSEALLFORM就会提示出错。
错误提示是:
“Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00403988 in module 'PROJECT1.EXE'. Read of address 00C7C000'. Process stopped. Use Step or Run to continue.”
查了下程序,问题出在CLOSEALLFORM中的:
if Assigned(Form3) then
begin
form3.Close;
Form3.Free;
form3 := nil;
end;
也就是说在释放FORM3时程序会出错,
搞不懂是怎么会事?是不是因为这时候QUICKREPORT还没释放?
如果在FORM3中没有对QUICKREPORT调用则不会出现这样的问题。
另,在PROJECT OPTION中FORM2、FORM3都不是自动创建的。