Just for the fun of it, here's a snippet of code I use periodically to add a tabsheet to a TPageControl that has a TMemo on it. This would be used, for example, if you've got a form that is used for editing text files. You'd call this to add a new tab with the filename as caption, then load the memo's .Line
property from the file's contents.
function TMy_form.add_ts_mmo( ntbk : TPageControl; caption : string ) : TTabSheet;
var mmo : TMemo;
begin
Result := TTabSheet.Create(self);
Result.PageControl := ntbk;
Result.Caption := caption;
mmo := TMemo.Create(self);
Result.Tag := Integer(mmo);
mmo.Parent := Result;
mmo.Font.Name := 'Courier New';
mmo.Font.Size := 10;
mmo.Align := alClient;
mmo.ScrollBars := ssBoth;
mmo.WordWrap := true;
end;
You call it by giving it the PageControl you want it to be added to, and a caption that's used in the tab.
var
ts : TTabSheet;
. . .
ts := add_ts_mmo( myPageControl, ExtractFileName( text_file_nm ) );
Note that I save the new memo's pointer in ts.Tag
so I can easily get to it later on through a cast.
TMemo(ts.Tag).Lines.LoadFromFile( text_file_nm );
No subclassing is required. You can create any other components you might want on the tabsheet as well, after the Result.Caption := caption
line. Just be sure to set their .Parent
property to Result
.
The PageControl can be created either at design time or run-time.