{"id":4518,"date":"2014-03-30T13:11:42","date_gmt":"2014-03-30T13:11:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dynamically-create-forms-using-a-string-collection-of-common-programming-errors\/"},"modified":"2014-03-30T13:11:42","modified_gmt":"2014-03-30T13:11:42","slug":"dynamically-create-forms-using-a-string-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/dynamically-create-forms-using-a-string-collection-of-common-programming-errors\/","title":{"rendered":"dynamically create forms using a STRING-Collection of common programming errors"},"content":{"rendered":"<p>Declare the NewForm variable as TForm:<\/p>\n<pre><code>var\n  NewForm: TForm;\nbegin\n  NewForm := TMyForm.Create(Tab1); \/\/compiles OK\n  NewForm := TMyOtherForm.Create(Tab2); \/\/also compiles OK\nend;\n<\/code><\/pre>\n<p>I&#8217;m assuming TMyForm and TMyOtherForm both are derivatives of TForm.<\/p>\n<h2>DRY<\/h2>\n<p>You can also reduce your repeating code using a class reference variable, like this:<\/p>\n<pre><code>procedure TForm1.ShowFormOnTab(pProcName:String);\nvar\n  NewForm: TForm;\n  ClassToUse: TFormClass;\n  NewTab: TTabSheet;\n  FormName: String;\n\nbegin\n  NewTab := TTabSheet.Create(PageControl1);\n  NewTab.PageControl:= PageControl1;\n  NewTab.Caption:='hi';\n  PageControl1.ActivePage :=  NewTab;\n\n  if pProcName='ProcfrmSetupItemCategories' then\n    ClassToUse := TfrmSetupItemCategories\n  else if pProcName='ProcfrmZones' then\n    ClassToUse := TfrmZones\n  else\n    ClassToUse := nil;\n  if Assigned(ClassToUse) then\n  begin\n    NewForm := ClassTouse.Create(NewTab);\n    NewTab.Caption := NewForm.Caption;\n    \/\/if you access custom properties or methods, this is the way:\n    if NewForm is TfrmZones then\n      TfrmZones(NewForm).ZoneInfo := 'MyInfo';\n  end;\nend;\n<\/code><\/pre>\n<h2>Register your classes and then create the forms from a string<\/h2>\n<p>As Sir Rufo points in his comment, you can even go further registering your classes (I&#8217;m not sure if this can be done in Lazarus, that exercise is up to you).<\/p>\n<p>First, register the form classes you want to instantiate from the class name, previous to any call to your ShowFormOnTab method, for example:<\/p>\n<pre><code>procedure TMainForm.FormCreate(Sender: TObject);\nbegin\n  RegisterClass(TfrmSetupItemCategories);\n  RegisterClass(TfrmZones);\n  \/\/and other classes\nend;\n<\/code><\/pre>\n<p>Then, you can change the code to get the class reference from the class name string:<\/p>\n<pre><code>procedure TForm1.ShowFormOnTab(pProcName:String);\nvar\n  NewForm: TForm;\n  ClassToUse: TFormClass;\n  ClassNameToUse: string;\n  NewTab: TTabSheet;\n  FormName: String;\n\nbegin\n  NewTab := TTabSheet.Create(PageControl1);\n  NewTab.PageControl:= PageControl1;\n  NewTab.Caption:='hi';\n  PageControl1.ActivePage :=  NewTab;\n  \/\/get rid of 'Proc' and add the T\n  \/\/or even better, pass directly the class name\n  ClassNameToUse := 'T' + Copy(pProcName, 5, MaxInt);\n  ClassToUse := TFormClass(FindClass(ClassNameToUse));\n\n  if Assigned(ClassToUse) then\n  begin\n    NewForm := ClassTouse.Create(NewTab);\n    NewTab.Caption := NewForm.Caption;\n    \/\/if you access custom properties or methods, this is the way:\n    if NewForm is TfrmZones then\n      TfrmZones(NewForm).ZoneInfo := 'MyInfo';\n  end;\nend;\n<\/code><\/pre>\n<p>That way, the code remains the same for any number of classes.<\/p>\n<p>For more info about this, take a look at Creating a Delphi form from a string in delphi.about.com.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4518","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4518"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4518\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}