【问题标题】:Strange Issue with TabSheet VisibleTabSheet 可见的奇怪问题
【发布时间】:2014-11-03 09:48:05
【问题描述】:

在 PageControl 上使用 TabSheets 并控制其可见性时,我已经克服了一些奇怪的行为。举个简单的例子,在 Form 上添加一个 PageControl,向该 PageControl 添加两个 TabSheet,为每个 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;

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

有没有更好的方法来控制这些可见性,也许为此使用 PageControl?

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

【问题讨论】:

  • 我已经使用过很多页面控件,而且我从来不需要弄乱 Tabsheet.visible,只是 tabsheet.Tabvisible。上面的 Cond1 和 Cond2 都返回 true,所以我不确定你的意图是什么。是否有时会隐藏这两者,或者总是显示其中之一?
  • 我试图提供一个简单的例子,所以在实际代码中 Cond1 和 Cond2 可以为真或假,并且稍微复杂一些。尽管如此,它确实可以在没有 Tabsheet.Visible 的情况下工作,并且它显示了正确的内容 - 所以我想我只需要摆脱这个。

标签: delphi delphi-xe3


【解决方案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;

【讨论】:

    猜你喜欢
    • 2010-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2018-06-26
    • 2011-08-13
    • 2011-08-17
    相关资源
    最近更新 更多