Mastodon
Programmierung

Putting controls on a page at runtime

Q:
Hi,
I need to create a component at runtime and ‘drop’ it
on a certain page of a multipage TNotebook – how
can this be done since the Tnotebook doesn’t expose
the pages – just their ‘names’.
For various reasons I do not want to a TPageControl.

A:
Well, i havn’t found an ‘easy’ way but this:

Use this function to get the TPage by it’s Caption:

[code lang=”delphi”]function GetPageByCaption(NoteBook:TNoteBook; Cap:string):TPage;
var
i : integer;
begin
result := nil;

for i := 0 to NoteBook.ControlCount-1 do
begin
if NoteBook.Controls[i] is TPage then
begin
if UpperCase(TPage(NoteBook.Controls[i]).Caption) = UpperCase(Cap)
then
begin
result := TPage(NoteBook.Controls[i]);
break;
end;
end;
end;
end;
[/code]
Then you could do something like this:

[code lang=”delphi”]var
ThePage : TPage;
begin
ThePage := GetPageByCaption(NoteBook1, ‘Page1’);
if ThePage <> nil then
begin
MyButton := TButton.Create(self);
MyButton.Parent := ThePage;
MyButton.Top := 10;
MyButton.Left := 20;
end;
end;
[/code]
have fun!
Boris Nienke

Edit: …Much easier way is this:
[code lang=”delphi”]begin
MyButton := TButton.Create(self);
with MyButton do
begin
Parent := Notebook1.Pages.Objects[0] As TWinControl;
Top := 10;
Left := 20;
end;
end;[/code]

[tags]Delphi, Components, Notebook[/tags]

0 Kommentare zu “Putting controls on a page at runtime

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.