那是自然!你叫它创建,它不敢不创建啊! if frmSaleTik <> nil then frmSaleTik := TfrmSaleTik.Create(self) else frmSaleTik.Show; 这段代码大有问题!自己参考Delphi自带的Demo吧!
var frmSaleTik : TfrmSaleTik; 放在这里 implementation
procedure TfrmMain.actSaleTikExecute(Sender: TObject); //var // frmSaleTik : TfrmSaleTik; begin if frmSaleTik <> nil then frmSaleTik := TfrmSaleTik.Create(self) else frmSaleTik.Show; end;
上面的多次创建问题已经基本解决,但是还是存在问题, 多次打开关闭,在多个窗口中交互操作,一会儿就出现错误提示了, "Abstract Error",还有access violation at 错误
用if not assign(form1) then form1=Tfomrm1.create else
procedure OpenForm(FormClass: TFormClass; var fm; AOwner: TComponent; fCaption: String); var i: Integer; Child: TForm; begin for i := 0 to Screen.FormCount - 1 do if Screen.Forms[i].ClassType = FormClass then begin Child := Screen.Forms[i]; if Child.WindowState = wsMinimized then ShowWindow(Child.Handle, SW_SHOWNORMAL) else ShowWindow(Child.Handle, SW_SHOWNA); if not Child.Visible then Child.Visible := True; Child.BringToFront; Child.SetFocus; TForm(fm) := Child; exit; end; Child := TForm(FormClass.NewInstance); TForm(fm) := Child; Child.Create(AOwner); Child.Caption := fCaption; end;
重复本人以上回答,以下在delphi5/delphi6下通过,绝对没问题!你在implementation之前定义生成子窗体定义过程,形式如下所示: var frmMain: TfrmMain;{主窗体} procedure OpenForm(FormClass: TFormClass; var fm; AOwner: TComponent; fCaption: String);{子窗体定义过程}implementation//uses dbMain, BaseJob;{$R *.dfm}procedure OpenForm(FormClass: TFormClass; var fm; AOwner: TComponent; fCaption: String); var i: Integer; Child: TForm; begin for i := 0 to Screen.FormCount - 1 do if Screen.Forms[i].ClassType = FormClass then begin Child := Screen.Forms[i]; if Child.WindowState = wsMinimized then ShowWindow(Child.Handle, SW_SHOWNORMAL) else ShowWindow(Child.Handle, SW_SHOWNA); if not Child.Visible then Child.Visible := True; Child.BringToFront; Child.SetFocus; TForm(fm) := Child; exit; end; Child := TForm(FormClass.NewInstance); TForm(fm) := Child; Child.Create(AOwner); Child.Caption := fCaption;{设置子窗体标题,也许你不需要} end; procedure TfrmMain.N7Click(Sender: TObject); begin {调用上述过程} OpenForm(TfrmBaseJob, frmBaseJob, Self, TMenuItem(Sender).Caption); {其中TMenuItem(Sender).Caption为你选中的菜单项标题} end;
补充一下:以上fm只为变量,无类型,使用时{TForm(fm) := Child;}强制转换
多次打开关闭,在多个窗口中交互操作,一会儿就出现错误提示了, "Abstract Error",还有access violation at 错误procedure TfrmMain.actSaleTikExecute(Sender: TObject); //var // frmSaleTik : TfrmSaleTik; begin if frmSaleTik <> nil then frmSaleTik := TfrmSaleTik.Create(self) else frmSaleTik.Show; end;在主窗口里定义了 fmSaleTik这个公有变量,在第一次生成这个子窗口的时候,是没有问题的,但当你关闭这个子窗口的时候(如果它的行为是caFree),那么这个子窗体所占用的资源便被释放掉,而这个时候,如果你没有FreeAndNil(fmSaleTik) ,这个变量(实际就是个指针)便指向一个空地址,当你再次调用上面的事件时,便会出现所提示的错误,因为这是个非法访问;
if frmSaleTik <> nil then
frmSaleTik := TfrmSaleTik.Create(self)
else
frmSaleTik.Show;
这段代码大有问题!自己参考Delphi自带的Demo吧!
frmSaleTik : TfrmSaleTik;
放在这里
implementation
//var
// frmSaleTik : TfrmSaleTik;
begin
if frmSaleTik <> nil then
frmSaleTik := TfrmSaleTik.Create(self)
else
frmSaleTik.Show;
end;
多次打开关闭,在多个窗口中交互操作,一会儿就出现错误提示了,
"Abstract Error",还有access violation at 错误
form1=Tfomrm1.create
else
var i: Integer;
Child: TForm;
begin
for i := 0 to Screen.FormCount - 1 do
if Screen.Forms[i].ClassType = FormClass then
begin
Child := Screen.Forms[i];
if Child.WindowState = wsMinimized then ShowWindow(Child.Handle, SW_SHOWNORMAL)
else ShowWindow(Child.Handle, SW_SHOWNA);
if not Child.Visible then Child.Visible := True;
Child.BringToFront;
Child.SetFocus;
TForm(fm) := Child;
exit;
end;
Child := TForm(FormClass.NewInstance);
TForm(fm) := Child;
Child.Create(AOwner);
Child.Caption := fCaption;
end;
var
frmMain: TfrmMain;{主窗体}
procedure OpenForm(FormClass: TFormClass; var fm; AOwner: TComponent; fCaption: String);{子窗体定义过程}implementation//uses dbMain, BaseJob;{$R *.dfm}procedure OpenForm(FormClass: TFormClass; var fm; AOwner: TComponent; fCaption: String);
var i: Integer;
Child: TForm;
begin
for i := 0 to Screen.FormCount - 1 do
if Screen.Forms[i].ClassType = FormClass then
begin
Child := Screen.Forms[i];
if Child.WindowState = wsMinimized then ShowWindow(Child.Handle, SW_SHOWNORMAL)
else ShowWindow(Child.Handle, SW_SHOWNA);
if not Child.Visible then Child.Visible := True;
Child.BringToFront;
Child.SetFocus;
TForm(fm) := Child;
exit;
end;
Child := TForm(FormClass.NewInstance);
TForm(fm) := Child;
Child.Create(AOwner);
Child.Caption := fCaption;{设置子窗体标题,也许你不需要}
end;
procedure TfrmMain.N7Click(Sender: TObject);
begin
{调用上述过程}
OpenForm(TfrmBaseJob, frmBaseJob, Self, TMenuItem(Sender).Caption);
{其中TMenuItem(Sender).Caption为你选中的菜单项标题}
end;
"Abstract Error",还有access violation at 错误procedure TfrmMain.actSaleTikExecute(Sender: TObject);
//var
// frmSaleTik : TfrmSaleTik;
begin
if frmSaleTik <> nil then
frmSaleTik := TfrmSaleTik.Create(self)
else
frmSaleTik.Show;
end;在主窗口里定义了 fmSaleTik这个公有变量,在第一次生成这个子窗口的时候,是没有问题的,但当你关闭这个子窗口的时候(如果它的行为是caFree),那么这个子窗体所占用的资源便被释放掉,而这个时候,如果你没有FreeAndNil(fmSaleTik) ,这个变量(实际就是个指针)便指向一个空地址,当你再次调用上面的事件时,便会出现所提示的错误,因为这是个非法访问;
第一:
var
frmSaleTik : TfrmSaleTik;
implementation
....
第二:
在MDI的OnDestory中写如下代码
frmSaleTik:=nil;
其它无变化!
哈哈,给分吧!!!