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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With