Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Issue with TabSheet Visible

I have overcome some strange behavior when working with TabSheets on a PageControl and controling their visibility. For a simple example, add a PageControl on a Form, add two TabSheets to that PageControl, add a Label to each TabSheet and assign the Forms OnCreate event.

The Code for OnCreate is like:

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;

As you can see, the Active page is still TabSheet1, but the content of TabSheet2 is displayed. Using the BringToFront, everything works as expected, but this seems quite odd to me.

Is there a better way to control these visibilities, maybe using the PageControl for this?

PS: I'm using VCL, not Firemonkey

like image 876
Sebastian Proske Avatar asked Dec 09 '25 21:12

Sebastian Proske


1 Answers

Remove assignments to TabSheet1.Visible and TabSheet2.Visible, those assignments mess up tabs visibility.

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;
like image 198
Dalija Prasnikar Avatar answered Dec 11 '25 14:12

Dalija Prasnikar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!