pagecontrol中关闭创建的tabsheet问题(50分)

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

seagull007

Unregistered / Unconfirmed
GUEST, unregistred user!
我想实现在菜单中点一个窗体,就显示在主界面的一个pagecontrol上,打开的窗体全部显示在pagecontrol上,每个form都有一个关闭按钮,通过关闭按钮关闭创建的tabsheet,我实现了加到pagecontrol上,但关闭老是报错,
1.创建pagecontrol的tabsheet,然后在它上边创建打开的form
procedure TfrmMain.CreateTabSheet( myFormClass: TFormClass;var Reference: TForm);
var
MyForm : TForm;
begin
tabsheet := TRzTabSheet.Create(RpgcMain);
try
tabsheet.Caption := Reference.Caption;
tabsheet.PageControl := RpgcMain;
tabsheet.Name := stabsheetname;
MyForm := myFormClass.Create(tabsheet);
try
MyForm.Parent := tabsheet;
MyForm.BorderStyle := bsnone;
MyForm.WindowState := wsmaximized;
RpgcMain.ActivePage := tabsheet;
MyForm.Show;
except
FreeAndNil(MyForm);
end;
except
FreeAndNil(tabsheet);
end;
end;
2.在主界面中写的关闭tabsheet方法,在各个form上都有一个关闭按钮,调用该方法
procedure TfrmMain.FreePageControl(pageindex:Integer);
var
ncount : Integer;
begin
ncount := Self.RpgcMain.PageCount;
if ncount > 0 then
begin
Self.RpgcMain.Pages[pageindex].Free;
if ncount > 1 then
Self.RpgcMain.ActivePageIndex := ncount - 2
else
Self.RpgcMain.Visible := False;
end;
end;
哪位兄弟晓得为啥,我搞了几天了,没找到问题在哪儿
 
在主面板或公共模块,写一个函数,关闭按钮调用它.
procedure SheetClose;
begin
with RpgcMaindo
begin
if PageCount>0 then
ActivePage.free;
Visible:=PageCount>0;
end;
end;
创建时,MyForm.WindowState := wsmaximized;最好用MyForm.align:=alClient
 
你的FreePageControl是在哪调用的?如果是formclose事件里那就会出错
procedure TfrmMain.FreePageControl(pageindex:Integer);
var
ncount : Integer;
begin
ncount := Self.RpgcMain.PageCount;
if ncount > 0 then
begin
Self.RpgcMain.Pages[pageindex].Free;
{这里,你都把form自己的父sheet free掉了,当然会出现重复释放出错啊,sheet在free的时候会自动先free内部的所有control的}
if ncount > 1 then
Self.RpgcMain.ActivePageIndex := ncount - 2
else
Self.RpgcMain.Visible := False;
end;
end;

建议你把关闭的部分改成发送一个自定义消息,然后再改消息事件里先free掉form再free掉sheet
 
FreePageControl是在哪调用的?
我是在每个form有一个关闭按钮,在关闭按钮中调用该方法的,并把当前的tabsheet的index传过来,formclose事件没有代码,
 
有哪位兄弟知道哦
 
你去点击的窗体一定是ActivePage,按我的去做就可以了.
 
to 大器晚成:兄弟,还是不行,可不可以我把程序发给你帮我调一下
 
MyForm := myFormClass.Create(tabsheet);
->
MyForm := myFormClass.Create(nil);
不要指定owner,如果指定了,释放tabsheet时候就会释放myform,如果再去释放myform就会有问题
 
这样也不行,
 
to hs-kill:我想用消息,但不会啊,能不能说说咋用哦
 
下面是我写的一个例子,可能和你的代码逻辑有点出入,希望对你有帮助:
const
WM_PCFORMCLOSE=WM_USER+100;
...
private
{ Private declarations }
procedure CreateFrmTabSheet;
procedure AFormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure PCFormClose(var msg:TMessage);
message WM_PCFORMCLOSE;
...
procedure TForm1.PCFormClose(var msg:TMessage);
begin
TForm(msg.LParam).Free;
TTabSheet(msg.WParam).Free;
end;

procedure TForm1.CreateFrmTabSheet;
var
i:integer;
st:TTabSheet;
frm:TForm;
begin
for i:=0 to 9do
begin
st:=TTabSheet.Create(PageControl1);
st.Caption:='TabSheet'+inttostr(i+1);
st.PageControl:=PageControl1;
frm:=TForm.Create(st);
frm.Caption:=st.Caption;
frm.OnCloseQuery:=AFormCloseQuery;
frm.Parent:=st;
frm.Show;
end;
end;

procedure TForm1.AFormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
CanClose:=false;
{这里不用关闭,非mainform执行close只不过是hide而已}
SendMessage(Handle,WM_PCFORMCLOSE,Integer(TForm(Sender).Parent),Integer(TForm(Sender)));
end;
 
to hs-kill:唉,晕,搞不定
 
?哪搞不定?
 
按F9运行,出错后停留在那?按F8
在可疑的地方设置断点(例如FreePageControl)中
 
to hs-kill:兄弟,可不可以帮我调一下,搞了几天了,我都不想弄了
 
我就想将打开的form放在pagecontrol上,通过每个form上的关闭按钮关闭form及创建的tabsheet,现在创建没问题,主要是关闭不行,
 
我上面的代码不就是这样吗? .......
 
不行,我试了,人都搞疲了,
 
那就是你的代码部分有问题.......把你自己写的代码 一段一段的注释掉,看看是哪部分出的问题
 
我只写一个主窗体,4个拿来测试的form,别的什么代码都没有,
这是主窗体代码:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, jpeg, ExtCtrls, RzTabs, RzPanel;

const
WM_PCFORMCLOSE = WM_USER + 100;
type
TfrmMain = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
N4: TMenuItem;
N5: TMenuItem;
RzPanel1: TRzPanel;
Image1: TImage;
RpgcMain: TRzPageControl;
sdfsdf1: TMenuItem;
procedure FormShow(Sender: TObject);
procedure N2Click(Sender: TObject);
procedure N3Click(Sender: TObject);
procedure N5Click(Sender: TObject);
procedure sdfsdf1Click(Sender: TObject);
private
{ Private declarations }
tabsheet : TRzTabSheet;
stabsheetname : String;
pagecontrolindex : Integer;
Function getPageControlSheetName(str:String):Boolean;
procedure CreateTabSheet(myFormClass: TFormClass;var Reference: TForm);
procedure AddTabSheet(tabname:String;myFormClass: TFormClass;var Reference: TForm);
procedure pagecontrolVisible;
public
{ Public declarations }
procedure FreePageControl;
procedure PCFormClose(var msg:TMessage);
message WM_PCFORMCLOSE;
end;

var
frmMain: TfrmMain;
activepagecontronindex : Integer;
//当前的index
implementation
uses cgrk, cgjh, Unit1, Unit2;
{$R *.dfm}
procedure TfrmMain.CreateTabSheet(myFormClass: TFormClass;var Reference: TForm);
var
MyForm : TForm;
begin
tabsheet := TRzTabSheet.Create(Nil);
tabsheet.Caption := Reference.Caption;
tabsheet.PageControl := RpgcMain;
tabsheet.Name := stabsheetname;
MyForm := myFormClass.Create(tabsheet);
MyForm.Parent := tabsheet;
MyForm.BorderStyle := bsnone;
MyForm.Align := alClient;
RpgcMain.ActivePage := tabsheet;
MyForm.Show;
end;

procedure TfrmMain.FormShow(Sender: TObject);
begin
Self.RpgcMain.Visible := False;
end;

procedure TfrmMain.FreePageControl;
var
ncount : Integer;
begin
ncount := Self.RpgcMain.PageCount;
if ncount > 0 then
begin
Self.RpgcMain.ActivePage.Free;
if ncount > 1 then
Self.RpgcMain.ActivePageIndex := ncount - 2
else
Self.RpgcMain.Visible := False;
end;
end;

function TfrmMain.getPageControlSheetName(str: String): Boolean;
var
i : Integer;
strName : String;
begin
Result := False;
For i := 0 to (Self.RpgcMain.PageCount - 1)do
begin
strName := Trim(Self.RpgcMain.Pages.Name);
pagecontrolindex := Self.RpgcMain.Pages.PageIndex;
if strName = Trim(str) then
//该名称已存在
begin
Result := True;
Break;
end;
end;
end;

procedure TfrmMain.N2Click(Sender: TObject);
begin
stabsheetname := 'tabsheetN2';
Self.AddTabSheet(stabsheetname,Tfrmcgrk,TForm(frmcgrk));
end;

procedure TfrmMain.N3Click(Sender: TObject);
begin
stabsheetname := 'tabsheetN3';
Self.AddTabSheet(stabsheetname,Tfrmcgjh,TForm(frmcgjh));
end;

procedure TfrmMain.pagecontrolVisible;
begin
if RpgcMain.Visible = false then
RpgcMain.Visible := true;
end;

procedure TfrmMain.N5Click(Sender: TObject);
begin
stabsheetname := 'tabsheetN5';
Self.AddTabSheet(stabsheetname,TForm1,TForm(Form1));
end;

procedure TfrmMain.sdfsdf1Click(Sender: TObject);
begin
stabsheetname := 'tabsheetsdfsdf1';
Self.AddTabSheet(stabsheetname,TForm2,TForm(Form2));
end;

procedure TfrmMain.AddTabSheet(tabname:String;myFormClass: TFormClass;
var Reference: TForm);
begin
if Self.getPageControlSheetName(tabname) then
//已经存在
begin
RpgcMain.ActivePageIndex := pagecontrolindex;
//显示出来
Self.pagecontrolVisible;
end
else
//创建
begin
Self.CreateTabSheet(myFormClass,Reference);
Self.pagecontrolVisible;
end;
end;

procedure TfrmMain.PCFormClose(var msg: TMessage);
begin
TForm(msg.LParam).Free;
TRzTabSheet(msg.WParam).Free;
end;

end.

这是每个form中关闭按钮的代码,别的什么代码也没有;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses Main;
{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
begin
showmessage(inttostr(frmMain.RpgcMain.ActivePageIndex));
SendMessage(frmMain.Handle,WM_PCFORMCLOSE,Integer(TForm(Sender).Parent),Integer(TForm(Sender)));
end;

end.
兄弟帮我看看是什么问题哦,今天搞不定就不想弄了,放几天再说
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
501
import
I
I
回复
0
查看
560
import
I
后退
顶部