Using TPageControl, how do I create a new page at run time?

Ok, I am writing an IRC client. Each IRC channel that the user is in will be a new tab (using TPageControl). Now, how do I create a new tab when a certain event is triggered? The tab would have to contain the same listboxes and all as the original tab (I created the original tab at design time). Any help would be great, I've been hammering at this all night and can't figure it out.



If you know how to do it using TTabControl please share that with me as well.



Thanks.


Comments

  • Both a TPageControl and TTabControl would work. Buth both will require a little planning. If you use a TPageControl you will have to create and destroy a few components as a user joins/parts a channel.

    But if you use a TTabControl you will have to share components, meaning the text from the current channel can be shown in your TRichEdit or TMemo (or whatever) but the other hidden channels' text will have to be stored in memory. Likewise for the nicklist, topic, channel modes, etc..



    Anyway, heres how you use both:



    TPageControl - This is straight out of the help file from the TPageControl.Pages screen:



    Note: Pages is a read-only property. To add a tab sheet to a page control at design time, right click and select New Page. To add a tab sheet to a page control at runtime, create the tab sheet and set its PageControl property to the page control.



    And TTabControl. Didnt need to look in the help file for this one. TTabControl.Tabs is a TString, so you use TTabControl.Tabs.Add('#somechannel') to add a new tab.



    : Ok, I am writing an IRC client. Each IRC channel that the user is in will be a new tab (using TPageControl). Now, how do I create a new tab when a certain event is triggered? The tab would have to contain the same listboxes and all as the original tab (I created the original tab at design time). Any help would be great, I've been hammering at this all night and can't figure it out.

    :

    : If you know how to do it using TTabControl please share that with me as well.

    :

    : Thanks.

    :






  • // I doubt if this will solve all your problems, but it might be a start:



    procedure TForm1.Button1Click(Sender: TObject);

    var

    NewTabSheet: TTabSheet;

    C:TControl;

    i:integer;

    begin

    NewTabSheet:=TTabSheet.Create(PageControl1);



    //NOTE - I would have never guessed this!

    NewTabSheet.PageControl:=PageControl1;



    NewTabSheet.Name:='TabSheet'+IntToStr(PageControl1.PageCount);

    NewTabSheet.Caption:=NewTabSheet.Name;



    // This next trick ( called "cloning" ) was adapted from

    // Marco Cantu's "Delphi Developer's Guide"

    for i:=0 to TabSheet1.ControlCount-1 do begin

    C:=TabSheet1.Controls[i];

    with TControlClass(C.ClassType).Create(NewTabSheet) do begin

    Parent:=NewTabSheet;

    SetBounds(C.Left,C.Top,C.Width,C.Height);



    // Need to add your other Properties and Events here...



    end;

    end;



    end;








  • : // I doubt if this will solve all your problems, but it might be a start:



    It solved all mine. Ive been working on an IRC client for awhile now. To get the project going, I set it up as a single window client. Although Im not using the "cloning" technique, it got me off in the right direction, and Ive finally cleared the last hurdle. (It was the typecasting examples from your code that helped)



    Thanks



    :

    : procedure TForm1.Button1Click(Sender: TObject);

    : var

    : NewTabSheet: TTabSheet;

    : C:TControl;

    : i:integer;

    : begin

    : NewTabSheet:=TTabSheet.Create(PageControl1);

    :

    : //NOTE - I would have never guessed this!

    : NewTabSheet.PageControl:=PageControl1;

    :

    : NewTabSheet.Name:='TabSheet'+IntToStr(PageControl1.PageCount);

    : NewTabSheet.Caption:=NewTabSheet.Name;

    :

    : // This next trick ( called "cloning" ) was adapted from

    : // Marco Cantu's "Delphi Developer's Guide"

    : for i:=0 to TabSheet1.ControlCount-1 do begin

    : C:=TabSheet1.Controls[i];

    : with TControlClass(C.ClassType).Create(NewTabSheet) do begin

    : Parent:=NewTabSheet;

    : SetBounds(C.Left,C.Top,C.Width,C.Height);

    :

    : // Need to add your other Properties and Events here...

    :

    : end;

    : end;

    :

    : end;

    :

    :

    :

    :






  • hehe

    12 Years later, it helped me!!
  • hehe

    12 Years later, it helped me!!
  • Everybody is good, we will discuss.[img=http://www.filii.info/g.gif]
  • Everybody is good, we will discuss.[img=http://www.filii.info/g.gif]
  • [link=http://www.iphonecasepackaging.com]iPhone case packaging[/link]

    [URL="http://www.iphonecasepackaging.com"]iPhone case packaging[/URL]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories