TabSheet可见的奇怪问题

时间:2014-11-03 09:48:05

标签: delphi delphi-xe3

在PageControl上使用TabSheets并控制其可见性时,我克服了一些奇怪的行为。举一个简单的例子,在Form上添加一个PageControl,向该PageControl添加两个TabSheets,为每个TabSheet添加一个Label并分配Forms OnCreate事件。

OnCreate的代码就像:

procedure TForm1.FormCreate(Sender: TObject);

  function Cond1: Boolean;
  begin
    result := 1=1;
  end;

  function Cond2: Boolean;
  begin
    result := 2=2;
  end;

begin
  TabSheet1.Visible := Cond1;
  TabSheet1.TabVisible := Cond1;
  if not (Cond1) then
    if PageControl1.ActivePage = TabSheet1 then
      PageControl1.ActivePage := TabSheet2;
  TabSheet2.Visible := Cond2;
  TabSheet2.TabVisible := Cond2;
  if not(Cond2) then
    if PageControl1.ActivePage = TabSheet2 then
      PageControl1.ActivePage := nil;
  ShowMessage(IntToStr(PageControl1.ActivePageIndex));
  //PageControl1.ActivePage.BringToFront;    //uncomment to work properly
end;

如您所见,活动页面仍为TabSheet1,但会显示TabSheet2的内容。 使用BringToFront,一切都按预期工作,但这对我来说似乎很奇怪。

有没有更好的方法来控制这些可见性,可能使用PageControl来实现这个目标?

PS:我使用的是VCL,而不是Firemonkey

1 个答案:

答案 0 :(得分:1)

删除TabSheet1.VisibleTabSheet2.Visible的作业,这些作业会影响标签的可见性。

begin
//  TabSheet1.Visible := Cond1;
  TabSheet1.TabVisible := Cond1;
  if not (Cond1) then
    if PageControl1.ActivePage = TabSheet1 then
      PageControl1.ActivePage := TabSheet2;
//  TabSheet2.Visible := Cond2;
  TabSheet2.TabVisible := Cond2;
  if not(Cond2) then
    if PageControl1.ActivePage = TabSheet2 then
      PageControl1.ActivePage := nil;
  ShowMessage(IntToStr(PageControl1.ActivePageIndex));
end;