JIMSMITH 0 Posted October 17 I have 5 frames that I want to create at runtime. The frames are descended from tframe2 or tframe3 how can I create this and allow the type (tframe2 or tframe3) to be specified in a variable.? So the object will be specified in a variable and the type will be in a variable as well. So tframe2 and tframe3 will be defined and created at runtime, but the actual frame and descendant from type would be in a variable. Share this post Link to post
Remy Lebeau 1403 Posted October 17 (edited) TFrame has a virtual constructor, so you can use a meta class variable for this task, eg: type TFrameClass = class of TFrame; var Frame: TFrame; FrameClass: TFrameClass; begin ... if SomeCondition then FrameClass := TFrame2; else FrameClass := TFrame3; Frame := FrameClass.Create(Owner); ... end; Edited October 17 by Remy Lebeau Share this post Link to post
Lars Fosdal 1792 Posted October 17 Note that the Frame instance should have its parent set to Owner as well. Share this post Link to post
Der schöne Günther 316 Posted October 17 Don't forget to set the Name property of your frame to something unique. Otherwise, it will throw a runtime exception when you have several components with identical name in the same container. Share this post Link to post
havrlisan 25 Posted October 17 (edited) 1 hour ago, Der schöne Günther said: Don't forget to set the Name property of your frame to something unique. Otherwise, it will throw a runtime exception when you have several components with identical name in the same container. Or to an empty string. It's the safest and easiest way, in my opinion. Edited October 17 by havrlisan 1 2 Share this post Link to post
Der schöne Günther 316 Posted October 17 Didn't even know that was possible. Thanks. 1 Share this post Link to post
Uwe Raabe 2059 Posted October 17 2 minutes ago, Der schöne Günther said: Didn't even know that was possible. It is even possible at design time. Setting an empty name to a component will remove the corresponding field in the class. It is often used with TLabel instances that only exist to display some static text, but won't be accessed in the code. It reduces code cluttering a bit. 3 3 Share this post Link to post
Remy Lebeau 1403 Posted October 17 (edited) 9 minutes ago, Uwe Raabe said: It is even possible at design time. Setting an empty name to a component will remove the corresponding field in the class. It is often used with TLabel instances that only exist to display some static text, but won't be accessed in the code. It reduces code cluttering a bit. Interesting! I never thought of that. I learned something new today 😁 I'll definitely have to play with this in my projects. Edited October 17 by Remy Lebeau Share this post Link to post
Typer2 0 Posted October 17 When using frame inheritance, be aware that running Delphi at high DPI can cause issues, which might negatively affect the layout and functionality of your frames. To avoid these issues, you can run Delphi in DPI-unaware mode: "C:\Program Files (x86)\Embarcadero\Studio\23.0\bin\bds.exe" /highdpi:unaware Share this post Link to post
tgbs 14 Posted October 17 2 hours ago, Uwe Raabe said: It is even possible at design time. Setting an empty name to a component will remove the corresponding field in the class. It is often used with TLabel instances that only exist to display some static text, but won't be accessed in the code. It reduces code cluttering a bit. This can be a problem in a multilingual project Share this post Link to post
mvanrijnen 123 Posted October 17 2 hours ago, tgbs said: This can be a problem in a multilingual project In that case, the text is not static 😉 1 Share this post Link to post
Lars Fosdal 1792 Posted October 18 21 hours ago, Der schöne Günther said: set the Name property of your frame to something unique I use a global function GetNextInstanceNumber to get and increment a integer variable and just set the name to Self.ClassName+'_'+GetNextInstanceNumber.ToString; Share this post Link to post