Search code examples
delphidelphi-2007

Delphi display a frame by tag number in page control


I have a unique question. I am using Delphi 2007 on Windows XP. I have a form with a TPageControl component. I created a Frame that I want to display within that PageControl. I will be creating many other frames that will be displayed based on button click events. Is there anyway to use the tag property of the frame so that when the button is pressed the tag number can be passed into a generic function or procedure so that the functions or procedures can be reused for all the buttons. Another idea was to use the tabsheets index property and match it to the frame tag number. Any suggestions would be great. Thanks in advance.


Solution

  • You need an function which maps the tag number to the frame class, something like following:

    type
      TFrameClass = class of TFrame;
    
    function GetFrameClass(const aClassID: Integer): TFrameClass;
    begin
      case aClassID of
        1 : Result := TFrameFoo;
        2 : Result := TFrameBar;
        else Result := nil;
      end;
    end;
    

    and then you can create frames:

    var FrClass: TFrameClass;
        Frame: TFrame;
    begin
      FrClass := GetFrameClass(btn.Tag);
      if(FrClass <> nil)then begin
         Frame := FrClass.Create(tabsheet);
         Frame.Parent := tabsheet;
      end;