Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:30:51+00:00 2026-06-15T16:30:51+00:00

I have 5 forms created at design time. I need to dynamically create an

  • 0

I have 5 forms created at design time. I need to dynamically create an instance of each form and put on a tab.

My question: If the form names are in an array of strings and I call my procedure like this:

ShowForm(FormName[3]);// To show the 3rd form on a tab page.

How can I define and create the new instance for each form?

This is what I have for now:

procedure TForm1.ShowFormOnTab(pProcName:String);
var
  NewForm: TfrmSetupItemCategories;//***HERE IS MY PROBLEM***

  NewTab: TTabSheet;
  FormName: String;

begin
  NewTab := TTabSheet.Create(PageControl1);
  NewTab.PageControl:= PageControl1;
  NewTab.Caption:='hi';
  PageControl1.ActivePage :=  NewTab;

  if pProcName='ProcfrmSetupItemCategories' Then
     begin
       NewForm:=TfrmSetupItemCategories.Create(NewTab);
       NewTab.Caption := NewForm.Caption;
     end;
  if pProcName='ProcfrmZones' Then
     begin
       NewForm:=TfrmZones.Create(NewTab);
       NewTab.Caption := NewForm.Caption;
     end;
.
.
.
end;

the line that reads “HERE IS MY PROBLEM” is where I need help. I can’t reuse NewForm as a variable with a second form in this way…

Note: My problem is NOT the tab. Rather it’s creating a new instance of the form using the same variable name.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:30:53+00:00Added an answer on June 15, 2026 at 4:30 pm

    Declare the NewForm variable as TForm:

    var
      NewForm: TForm;
    begin
      NewForm := TMyForm.Create(Tab1); //compiles OK
      NewForm := TMyOtherForm.Create(Tab2); //also compiles OK
    end;
    

    I’m assuming TMyForm and TMyOtherForm both are derivatives of TForm.

    DRY

    You can also reduce your repeating code using a class reference variable, like this:

    procedure TForm1.ShowFormOnTab(pProcName:String);
    var
      NewForm: TForm;
      ClassToUse: TFormClass;
      NewTab: TTabSheet;
      FormName: String;
    
    begin
      NewTab := TTabSheet.Create(PageControl1);
      NewTab.PageControl:= PageControl1;
      NewTab.Caption:='hi';
      PageControl1.ActivePage :=  NewTab;
    
      if pProcName='ProcfrmSetupItemCategories' then
        ClassToUse := TfrmSetupItemCategories
      else if pProcName='ProcfrmZones' then
        ClassToUse := TfrmZones
      else
        ClassToUse := nil;
      if Assigned(ClassToUse) then
      begin
        NewForm := ClassTouse.Create(NewTab);
        NewTab.Caption := NewForm.Caption;
        //if you access custom properties or methods, this is the way:
        if NewForm is TfrmZones then
          TfrmZones(NewForm).ZoneInfo := 'MyInfo';
      end;
    end;
    

    Register your classes and then create the forms from a string

    As Sir Rufo points in his comment, you can even go further registering your classes (I’m not sure if this can be done in Lazarus, that exercise is up to you).

    First, register the form classes you want to instantiate from the class name, previous to any call to your ShowFormOnTab method, for example:

    procedure TMainForm.FormCreate(Sender: TObject);
    begin
      RegisterClass(TfrmSetupItemCategories);
      RegisterClass(TfrmZones);
      //and other classes
    end;
    

    Then, you can change the code to get the class reference from the class name string:

    procedure TForm1.ShowFormOnTab(pProcName:String);
    var
      NewForm: TForm;
      ClassToUse: TFormClass;
      ClassNameToUse: string;
      NewTab: TTabSheet;
      FormName: String;
    
    begin
      NewTab := TTabSheet.Create(PageControl1);
      NewTab.PageControl:= PageControl1;
      NewTab.Caption:='hi';
      PageControl1.ActivePage :=  NewTab;
      //get rid of 'Proc' and add the T
      //or even better, pass directly the class name
      ClassNameToUse := 'T' + Copy(pProcName, 5, MaxInt);
      ClassToUse := TFormClass(FindClass(ClassNameToUse));
    
      if Assigned(ClassToUse) then
      begin
        NewForm := ClassTouse.Create(NewTab);
        NewTab.Caption := NewForm.Caption;
        //if you access custom properties or methods, this is the way:
        if NewForm is TfrmZones then
          TfrmZones(NewForm).ZoneInfo := 'MyInfo';
      end;
    end;
    

    That way, the code remains the same for any number of classes.

    For more info about this, take a look at Creating a Delphi form from a string in delphi.about.com.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a tab control and few forms. Each form opens in a
I have created two forms in my Windows Application. One Form acts as a
I have created Login forms and registration forms for a website. The form is
I have a form created in Windows Forms which is draggable wherever I click.
I need help with the following design. Basically I have a Main form which
I have created a program with Windows Forms in C# and the architecture is
i have created an mvc3 web application that uses forms based authentication. one part
I have an MVC application created from the Internet template to use forms authentication,
I have a C# Windows Forms application, whose prototype was created on SQL Server
When you have an ASP.Net MVC form created by Html.BeginForm(), how do fields inside

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.