There is another way that I learned a number of years ago - place each tab on a different form. "What? But that's nonsense Steve, you've been smoking something!", I hear you say. Not so. Follow along for a tricky bit of coding.
Create your form with all your tabs - just don’t put anything in them for the moment. Now for each tab, create a totally separate form, place a TPanel on the form and align it to Client. Give that Panel the same name for each form so you'll remember it, like MainPanel. Then create all your components onto the MainPanel and add your logic code to that form.
Now, going back to the main form with all the tabs, in the OnChange event of the Tabs, place something like the following code...
procedure TfrmMain.pcMainTabChange(Sender: TObject);
begin
case pcMainTab.TabIndex of
1: FrmOne.MainPanel.Parent := tsOne;
2: FrmTwo.MainPanel.Parent := tsTwo;
3: FrmThree.MainPanel.Parent := tsThree;
end;
end;
And there you have it. Simple isn't it?
the tsOne, tsTwo etc are the names you can give to the individual TTabSheet you want the form's components to appear on.
What is happening here is that you are changing the parent of the MainPanel to the TTabSheet of the tab you want it to appear in. By changing the parent, you are actually telling it that it now lives on that new tabsheet instead of the form. All the code will still apply.
All the logic code for each tab is in a separate form, kept tidy and easier to follow.
You can add code to create the form at runtime if you wish, but remember to free it again in the OnChanging event.
God bless and have a wonderful day.
Having identified this as a "best practice", Borland (now CodeGear) created Frames for this kind of thing - which allow you to drop them onto your form as complex components :-)
ReplyDeleteEven better! Thanks.
ReplyDelete