0

我想创建一个必须在运行时创建的 TTabsheet。TTabSheet 有几个组件,但所有这些组件在每个选项卡上都是相同的。是否可以创建一个“类型”变量,每次都会创建这些选项卡?

谢谢

4

2 回答 2

2

是的。您可以从 TTabSheet 创建继承的类

TCustomTabSheet = class(TTabSheet)
public
  constructor Create(AOwner : TComponent); override;
public
   FTestButton : TButton;
end;

constructor TCustomTabSheet.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
  FTestButton := TButton.Create(Self);
  FTestButton.Parent := Self;
  FTestButton.Left := 1;
  FTestButton.Top := 1;
  FTestButton.Width := 20;
  FTestButton.Heigth := 10;
  FTestButton.Caption := 'Cool button!';
  FTestButton.Name := 'TestButton';
end;

您还可以在设计时使用自定义控件创建一个框架 (TFrame),并将其实例托管到所有新选项卡。

于 2014-11-28T12:51:47.777 回答
0

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.

于 2014-11-28T19:03:21.847 回答