Delphi - Frames - Dymanically removing and adding

Posted on 2006-04-27
Last Modified: 2008-03-17

Just using a basic scenario, lets assume that we have a single form with 2 buttons.  One button says 'display frame one' while the other says 'display frame two'.  In our project, we also have 2 frames (FrameOne and FrameTwo).

How do I display FrameOne when the first button is clicked, and then remove it and display FrameTwo when the second button is clicked.  I think I have the 'remove old frame' right, so I basically just need the 'create new frame' method.  In both cases, the frames are the same size and should be displayed on the same location on the form.
Question by:Sebastion
    LVL 15

    Accepted Solution

    I used a groupbox to display the frames inside

    procedure TForm1.Button1Click(Sender: TObject);
      if frame1 = nil then
        frame1 := Tframe1.Create(GroupBox1);

      frame1.Parent := GroupBox1;
      frame1.Align := alClient;

    procedure TForm1.Button2Click(Sender: TObject);
      if frame2 = nil then
        frame2 := Tframe2.Create(GroupBox1);

      frame2.Parent := GroupBox1;
      frame2.Align := alClient;

    Author Comment

    Hey Mike,

    From what I gather, you're using two different variables (frame1 and frame2).  Is it possible to use just a single variable declared as a TFrame?

    Also, with this method, are you just overwriting frame1 with frame2 (and vis versa), or are you freeing up the frame before writing in the new one?

    Author Comment

    I guess what I mean is whether .free is enough when closing a frame, before opening another.
    LVL 15

    Expert Comment

    Yes I am using 2 different frames, but I just assumed that would be what you are doing.
    If your using the same frame twice, you would just need to referesh visual details, but then it wouldnt be worth using frames at all.

    Yes you can just Free and Nil the frame to clear the variables, depends how often you are switching as to whether you want to keep recreating the form rather than updating on form components.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
    Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now