我自己想做一个个人用的多页面笔记本程序 但是遇到了一个奇怪的问题 我运行程序后 第一次打开OpenDialog没有问题,第二次打开OpenDialog然后把鼠标指向一个文件后,只要文件的HINT一出现,就提示出错。(如果我在不出现HINT之前打开文件就没事。)我搞不懂为什么会这样 TTabSheetForBook是一个自己添加了几个其他变量的TTabSheet的拓展类
原来的代码是
procedure TMForm.Menu_File_OpenClick(Sender: TObject);
label ne;
var
NewMemo:TMemo;
i,j:integer;
S,Err:string;
begin
try
MainPageControl.DisableAlign;
try {except}
if OpenDialog.Execute then
begin
for i:=0 to OpenDialog.Files.Count-1 do
begin
s:=OpenDialog.Files.Strings[i];
for j:=0 to MainPageControl.PageCount-1 do
begin
if s=TTabSheetForBook(MainPageControl.Pages[j]).FileName then
begin
Err:=Err+'文件 “'+s+'”已经被本程序打开了!'+#13;
goto ne;
end;
end;
if fileexists(s) then
begin
if (MainPageControl.PageCount=1) and (MainPageControl.ActivePage.Caption=NothingPage) and //这里 NothingPage是字符变量"空白文档"
(TTabSheetForBook(MainPageControl.ActivePage).FileName='') then //如果当前页面数量只有1页,并且该页的名称是 NothingPage 并且 没有对应的文件的话
begin
MainPageControl.ActivePage.Caption:=NoExtName(s); //把标题的名字改成文件的名字 NoExtName是去除后缀
FindMemoInTab(TTabSheetForBook(MainPageControl.ActivePage)).Lines.LoadFromFile(s); //把文本载入这个组件
FindMemoInTab(TTabSheetForBook(MainPageControl.ActivePage)).Modified:=false;
MainPageControl.ActivePage.Caption:=copy(MainPageControl.ActivePage.Caption,3,length(MainPageControl.ActivePage.Caption)); TTabSheetForBook(MainPageControl.ActivePage).FileName:=s;
end
else //对应 if (MainPageControl.PageCount=1) and (MainPageCon.......
begin
NewMemo:=NewPage(NoExtName(s),OpenDialog.FileName,MainPageControl);
NewMemo.Lines.LoadFromFile(s);
NewMemo.Modified:=false;
MainPageControl.ActivePage.Caption:=copy(MainPageControl.ActivePage.Caption,3,length(MainPageControl.ActivePage.Caption));
TTabSheetForBook(MainPageControl.ActivePage).FileName:=s;
end;
end;
ne:
end; {for}
end;
except
Messagebox(self.handle,'打开文件时发生错误!','错误',mb_ok or MB_ICONHAND);
exit;
end;
finally
MainPageControl.EnableAlign;
if Err<>'' then messagebox(self.Handle,pchar(Err),'提示',MB_ICONASTERISK or mb_ok);
end;
end;我甚至把代码改成procedure TMForm.Menu_File_OpenClick(Sender: TObject);
label ne;
var
NewMemo:TMemo;
i,j:integer;
S,Err:string;
begin
try
MainPageControl.DisableAlign;
try {except}
if OpenDialog.Execute then
begin
end;
except
Messagebox(self.handle,'打开文件时发生错误!','错误',mb_ok or MB_ICONHAND);
exit;
end;
finally
MainPageControl.EnableAlign;
if Err<>'' then messagebox(self.Handle,pchar(Err),'提示',MB_ICONASTERISK or mb_ok);
end;
end;
就这样也一样 第二次打开OpenDialog后把鼠标指向一个文件或者文件夹 然后只要HINT一出现 就出错!下面是自定义类的代码
unit NewClassU;interface
uses
Graphics, Controls, SysUtils, Classes, ComCtrls, Windows,
Dialogs, Forms, StdCtrls;function NewPage(Caption: WideString; FilePath:WideString; APageControl: TPageControl): TMemo;
type
TTabSheetForBook = class(TTabSheet) //TTabSheetForNeotoma为TTabSheet的子类。
private
{FLock: Boolean;
FUnlockPassword: WideString; }
FMemoName: String;
FFileName: WideString; //用来记录文件绝对路径的
FEncode:Boolean;
{FNote: WideString;
FCanClose: Boolean; }
procedure SetFileName(const Value: WideString);
procedure SetMemoName(const Value: String);
procedure SetEncode(const Value: Boolean=false);
{procedure SetLock(const Value: Boolean);
procedure SetUnlockPassword(const Value: WideString);
procedure SetNote(const Value: WideString);
procedure SetCanClose(const Value: Boolean); }
published
property Encode: Boolean read FEncode write SetEncode;
property MemoName: String read FMemoName write SetMemoName;
property FileName: WideString read FFileName write SetFileName; //可以用来保存文件名或其它字符串信息。
{property UnlockPassword: WideString read FUnlockPassword write SetUnlockPassword; //解锁密码。
property Lock: Boolean read FLock write SetLock Default false; //是否被锁定。
property Note: WideString read FNote write SetNote; //可以用来保存字符串信息。
property CanClose: Boolean read FCanClose write SetCanClose; //是否可关闭。也可用于保存任意Boolean信息。 }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
protected
end;var
MemoCount:Integer;implementationuses MainForm;destructor TTabSheetForBook.Destroy;
begin
inherited;
end;constructor TTabSheetForBook.Create(AOwner: TComponent);
begin
inherited;
{Lock := false;
UnLockPassword := '';
CanClose := true; }
end;procedure TTabSheetForBook.SetFileName(const Value: WideString);
begin
FFileName := Value;
end;procedure TTabSheetForBook.SetMemoName(const Value: String);
begin
FMemoName := Value;
end;procedure TTabSheetForBook.SetEncode(const Value: Boolean=false);
begin
FEncode :=Value;
end;
function NewPage(Caption: WideString; FilePath:WideString; APageControl: TPageControl): TMemo;
var
NewPage: TTabSheetForBook;
NewMemo: TMemo;
begin
try
NewPage := TTabSheetForBook.Create(APageControl);
NewPage.PageControl := APageControl;
NewPage.Align := alClient;
//NewPage.ShowHint := false;
//NewPage.Hint := Caption;
NewPage.Caption := Caption;
NewPage.FileName:=FilePath;
//NewPage.Brush.Color := clGray; //灰色背景。 NewMemo:= TMemo.Create(NewPage);
NewMemo.Parent := NewPage;
NewMemo.Align := alClient;
NewMemo.Name:='Memo'+inttostr(MemoCount);
NewMemo.Lines.Clear;
NewMemo.ScrollBars := ssBoth;
NewMemo.HideSelection := false;
NewMemo.OnMouseDown:=MForm.MemoMouseDown;
NewMemo.OnChange:=MForm.MemoChange;
NewMemo.OnKeyUp:=MForm.MemoKeyUp;
NewMemo.HideSelection:=false;
{NewRichEdit.PopupMenu := Frm_Main.PopupMenuForRichEdit;
NewRichEdit.Modified := false;
NewRichEdit.OnChange := Frm_Main.RichEditOnChange;
NewRichEdit.Modified := false;
NewRichEdit.OnMouseDown := Frm_Main.RichEdit_OnMouseDown;}
NewPage.MemoName:=NewMemo.Name;
APageControl.ActivePage := NewPage; //设置为当前页面。
Result := NewMemo;
except
Result := nil;
messagebox(MForm.Handle,'调用函数 NewPage 时发生错误!','错误',MB_ICONHAND or mb_ok);
end;end;
end.我都不知道为什么会这样 希望各位帮忙一起分析!
原来的代码是
procedure TMForm.Menu_File_OpenClick(Sender: TObject);
label ne;
var
NewMemo:TMemo;
i,j:integer;
S,Err:string;
begin
try
MainPageControl.DisableAlign;
try {except}
if OpenDialog.Execute then
begin
for i:=0 to OpenDialog.Files.Count-1 do
begin
s:=OpenDialog.Files.Strings[i];
for j:=0 to MainPageControl.PageCount-1 do
begin
if s=TTabSheetForBook(MainPageControl.Pages[j]).FileName then
begin
Err:=Err+'文件 “'+s+'”已经被本程序打开了!'+#13;
goto ne;
end;
end;
if fileexists(s) then
begin
if (MainPageControl.PageCount=1) and (MainPageControl.ActivePage.Caption=NothingPage) and //这里 NothingPage是字符变量"空白文档"
(TTabSheetForBook(MainPageControl.ActivePage).FileName='') then //如果当前页面数量只有1页,并且该页的名称是 NothingPage 并且 没有对应的文件的话
begin
MainPageControl.ActivePage.Caption:=NoExtName(s); //把标题的名字改成文件的名字 NoExtName是去除后缀
FindMemoInTab(TTabSheetForBook(MainPageControl.ActivePage)).Lines.LoadFromFile(s); //把文本载入这个组件
FindMemoInTab(TTabSheetForBook(MainPageControl.ActivePage)).Modified:=false;
MainPageControl.ActivePage.Caption:=copy(MainPageControl.ActivePage.Caption,3,length(MainPageControl.ActivePage.Caption)); TTabSheetForBook(MainPageControl.ActivePage).FileName:=s;
end
else //对应 if (MainPageControl.PageCount=1) and (MainPageCon.......
begin
NewMemo:=NewPage(NoExtName(s),OpenDialog.FileName,MainPageControl);
NewMemo.Lines.LoadFromFile(s);
NewMemo.Modified:=false;
MainPageControl.ActivePage.Caption:=copy(MainPageControl.ActivePage.Caption,3,length(MainPageControl.ActivePage.Caption));
TTabSheetForBook(MainPageControl.ActivePage).FileName:=s;
end;
end;
ne:
end; {for}
end;
except
Messagebox(self.handle,'打开文件时发生错误!','错误',mb_ok or MB_ICONHAND);
exit;
end;
finally
MainPageControl.EnableAlign;
if Err<>'' then messagebox(self.Handle,pchar(Err),'提示',MB_ICONASTERISK or mb_ok);
end;
end;我甚至把代码改成procedure TMForm.Menu_File_OpenClick(Sender: TObject);
label ne;
var
NewMemo:TMemo;
i,j:integer;
S,Err:string;
begin
try
MainPageControl.DisableAlign;
try {except}
if OpenDialog.Execute then
begin
end;
except
Messagebox(self.handle,'打开文件时发生错误!','错误',mb_ok or MB_ICONHAND);
exit;
end;
finally
MainPageControl.EnableAlign;
if Err<>'' then messagebox(self.Handle,pchar(Err),'提示',MB_ICONASTERISK or mb_ok);
end;
end;
就这样也一样 第二次打开OpenDialog后把鼠标指向一个文件或者文件夹 然后只要HINT一出现 就出错!下面是自定义类的代码
unit NewClassU;interface
uses
Graphics, Controls, SysUtils, Classes, ComCtrls, Windows,
Dialogs, Forms, StdCtrls;function NewPage(Caption: WideString; FilePath:WideString; APageControl: TPageControl): TMemo;
type
TTabSheetForBook = class(TTabSheet) //TTabSheetForNeotoma为TTabSheet的子类。
private
{FLock: Boolean;
FUnlockPassword: WideString; }
FMemoName: String;
FFileName: WideString; //用来记录文件绝对路径的
FEncode:Boolean;
{FNote: WideString;
FCanClose: Boolean; }
procedure SetFileName(const Value: WideString);
procedure SetMemoName(const Value: String);
procedure SetEncode(const Value: Boolean=false);
{procedure SetLock(const Value: Boolean);
procedure SetUnlockPassword(const Value: WideString);
procedure SetNote(const Value: WideString);
procedure SetCanClose(const Value: Boolean); }
published
property Encode: Boolean read FEncode write SetEncode;
property MemoName: String read FMemoName write SetMemoName;
property FileName: WideString read FFileName write SetFileName; //可以用来保存文件名或其它字符串信息。
{property UnlockPassword: WideString read FUnlockPassword write SetUnlockPassword; //解锁密码。
property Lock: Boolean read FLock write SetLock Default false; //是否被锁定。
property Note: WideString read FNote write SetNote; //可以用来保存字符串信息。
property CanClose: Boolean read FCanClose write SetCanClose; //是否可关闭。也可用于保存任意Boolean信息。 }
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
protected
end;var
MemoCount:Integer;implementationuses MainForm;destructor TTabSheetForBook.Destroy;
begin
inherited;
end;constructor TTabSheetForBook.Create(AOwner: TComponent);
begin
inherited;
{Lock := false;
UnLockPassword := '';
CanClose := true; }
end;procedure TTabSheetForBook.SetFileName(const Value: WideString);
begin
FFileName := Value;
end;procedure TTabSheetForBook.SetMemoName(const Value: String);
begin
FMemoName := Value;
end;procedure TTabSheetForBook.SetEncode(const Value: Boolean=false);
begin
FEncode :=Value;
end;
function NewPage(Caption: WideString; FilePath:WideString; APageControl: TPageControl): TMemo;
var
NewPage: TTabSheetForBook;
NewMemo: TMemo;
begin
try
NewPage := TTabSheetForBook.Create(APageControl);
NewPage.PageControl := APageControl;
NewPage.Align := alClient;
//NewPage.ShowHint := false;
//NewPage.Hint := Caption;
NewPage.Caption := Caption;
NewPage.FileName:=FilePath;
//NewPage.Brush.Color := clGray; //灰色背景。 NewMemo:= TMemo.Create(NewPage);
NewMemo.Parent := NewPage;
NewMemo.Align := alClient;
NewMemo.Name:='Memo'+inttostr(MemoCount);
NewMemo.Lines.Clear;
NewMemo.ScrollBars := ssBoth;
NewMemo.HideSelection := false;
NewMemo.OnMouseDown:=MForm.MemoMouseDown;
NewMemo.OnChange:=MForm.MemoChange;
NewMemo.OnKeyUp:=MForm.MemoKeyUp;
NewMemo.HideSelection:=false;
{NewRichEdit.PopupMenu := Frm_Main.PopupMenuForRichEdit;
NewRichEdit.Modified := false;
NewRichEdit.OnChange := Frm_Main.RichEditOnChange;
NewRichEdit.Modified := false;
NewRichEdit.OnMouseDown := Frm_Main.RichEdit_OnMouseDown;}
NewPage.MemoName:=NewMemo.Name;
APageControl.ActivePage := NewPage; //设置为当前页面。
Result := NewMemo;
except
Result := nil;
messagebox(MForm.Handle,'调用函数 NewPage 时发生错误!','错误',MB_ICONHAND or mb_ok);
end;end;
end.我都不知道为什么会这样 希望各位帮忙一起分析!
if OpenDialog.Execute then
begin
end;
except
on E:Exception do
begin
Messagebox(self.handle,PAnsiChar('打开文件时发生错误!' + E.Message),'错误',mb_ok or MB_ICONHAND);
exit;
end;
end;