TNoteBook是一个组件,它提供了一个容器来容纳排列在页面中的各种控件,就像现实世界的笔记本一样。
TNotebook 的所有页面都由 Pages 属性列出,这是一个包含页面名称的TStrings类。还有一个属性 Page(注意单数!)列出了可以插入其他控件的 TPage 控件。与 TPageControl 不同,TNotebook 没有选项卡。因此,页面之间的导航不像TPageControl那样容易:
procedure TForm1.ForwardBtnClick(Sender: TObject);
begin
if Notebook1.PageIndex < Notebook1.PageCount-1 then
Notebook1.PageIndex := Notebook1.PageIndex + 1;
end;
procedure TForm1.BackwardBtnClick(Sender: TObject);
begin
if Notebook1.PageIndex > 0 then
Notebook1.PageIndex := Notebook1.PageIndex - 1;
end;
可以在设计时通过右键单击 TNotebook 组件并从上下文菜单中 选择添加页面来添加页面。
在运行时,通过调用 Notebook.Add('new pagename') 以新页面的名称作为参数来添加页面;该函数返回新页面的索引。如前所述,新页面本身可以通过使用属性 Notebook.Page[newindex](注意单数)来寻址。以下示例添加一个新页面并在其上放置一个TMemo:
procedure TForm1.NewPageBtnClick(Sender: TObject);
var
newIndex: Integer;
begin
newIndex := Notebook1.Pages.Add('New Page'); // use plural!
with TMemo.Create(self) do
begin
Parent := Notebook1.Page[newIndex]; // use singular!
Align := alClient;
end;
end;
在设计时,可以通过使用 TNotebook 的上下文菜单并选择菜单项 Delete Page 来删除页面。
在运行时,通过从 Pages 列表中删除相应的索引(复数!)或通过破坏特定索引处的Page[index] (单数!)来删除页面。
procedure TForm1.DeleteCurrPageBtn(Sender: TObject);
begin
if Notebook1.PageIndex > -1 then
Notebook1.Pages.Delete(Notebook1.PageIndex);
// or: Notebook1.Page[Notebook1.PageIndex].Free
end;
end;
对于 MPI 窗体,其典型的实现是使用 TPageControl 组件,通过 TNotebook 组件也可以实现,但是该组件没有选项卡,必须编写代码才能在选项卡之间切换。 类似于使用 TPageControl,我们通过一个函数来实现 MPI 窗体的切换,代码如下:
procedure TMainForm.ShowFormInNotebook(Form: TForm);
var
i: Integer;
begin
// 检查窗体是否已经打开,如果已经打开,则切换到对应的窗体
for i:=0 to MainNotebook.PageCount - 1 do
begin
Page:=MainNotebook.Page[i];
if Page.Caption = Form.Caption then
begin
MainNotebook.PageIndex:=i;
Exit;
end;
end;
// 如果未打开,则将窗体添加到选项卡中,并显示
i:=MainNotebook.Pages.Add(Form.Caption);
Form.Parent:=MainNotebook.Page[i];
Form.BorderStyle := bsNone;
Form.Top := 0;
Form.Left := 0;
Form.Width := MainNotebook.Width;
Form.Height := MainNotebook.Height;
Form.Align := alClient;
Form.Show;
end;
procedure TMainForm.UserBitBtnClick(Sender: TObject);
begin
// 用户管理
ShowFormInNotebook(UserForm);
end;
procedure TMainForm.ExpertTypeBitBtnClick(Sender: TObject);
begin
// 专业管理
ShowFormInNotebook(ExpertTypeForm);
end;
留言与评论(共有 0 条评论) “” |