JIMSMITH 0 Posted yesterday at 01:32 AM 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 1360 Posted 23 hours ago (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 22 hours ago by Remy Lebeau Share this post Link to post
Lars Fosdal 1775 Posted 18 hours ago 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 314 Posted 15 hours ago 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 20 Posted 13 hours ago (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 13 hours ago by havrlisan 1 2 Share this post Link to post
Der schöne Günther 314 Posted 13 hours ago Didn't even know that was possible. Thanks. 1 Share this post Link to post
Uwe Raabe 2028 Posted 13 hours ago 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 2 Share this post Link to post
Remy Lebeau 1360 Posted 13 hours ago (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 13 hours ago by Remy Lebeau Share this post Link to post
Typer2 0 Posted 11 hours ago 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 11 Posted 11 hours ago 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 8 hours ago 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