Make components on form private lazarus / 31-LCL ---------------------------------------------------------------------- Turbonobo · 2016-05-08 10:44 Hi, If I want to restrict the access to components on a form. (for encapsulation and code sanity). I could declare them as 'private', but then I wouldn't be able to use the form designer (no RTTI for private members, makes sense). Is there a workaround for this? (besides doing everything in code?) Could I for example use the form designer on a base class and make the components 'private'  or hidden in a derived class? Not really a problem, just wondering... ---------------------------------------------------------------------- howardpc · 2016-05-08 11:26 Creating controls and setting their properties can be done either manually in code, or 'automatically' using the RAD Designer and .lfm files. If you use the Designer and want controls created and their properties set at run time you have no alternative to making those controls and their events published. The whole publishing/property streaming mechanism is designed to enable that sort of RAD approach. RTTI is an essential ingredient in the process. If you reduce visibility of those controls, even making them public rather than published, then the run time class and property loading process falls over (there is no longer RTTI data to access, so even the class names cannot be found, nor the property/event values), and nothing gets created. But nothing prevents you from prototyping your form with its controls using the Designer, and then later using the .lfm data to construct the whole thing in code in a new  form's OnCreate event, reproducing the 'same' controls and using hard-coded values copied from the former .lfm. ---------------------------------------------------------------------- HeavyUser · 2016-05-08 21:02 Quote from: Turbonobo on May 08, 2016, 12:44:24 pmHi, If I want to restrict the access to components on a form. (for encapsulation and code sanity). I could declare them as 'private', but then I wouldn't be able to use the form designer (no RTTI for private members, makes sense). Is there a workaround for this? (besides doing everything in code?) No. Well almost you could write a small IDE addin to help you convert a number of selected components to code and copy it in memory allowing you to place it where ever you need. Then you delete the components from the designer and move their declaration in to private just before releasing a stable version and you have (kind of) both worlds. It smells like c/c++ but hey it works. Quote from: Turbonobo on May 08, 2016, 12:44:24 pmCould I for example use the form designer on a base class and make the components 'private'  or hidden in a derived class? Not really a problem, just wondering...  No. You can not lower the visibility of an ancestor class sorry. ---------------------------------------------------------------------- Turbonobo · 2016-05-09 12:21 Quote from: HeavyUser on May 08, 2016, 11:02:11 pmNo. Well almost you could write a small IDE addin to help you convert a number of selected components to code and copy it in memory allowing you to place it where ever you need. Then you delete the components from the designer and move their declaration in to private just before releasing a stable version and you have (kind of) both worlds. It smells like c/c++ but hey it works. I was considering this... but I was hoping someone knew a better solution. Gexperts has a tool that does this for Delphi http://www.gexperts.org/tour/index.html?components_to_code.html But I think I found a solution: Quote from: HeavyUser on May 08, 2016, 11:02:11 pmNo. You can not lower the visibility of an ancestor class sorry. But I can do it the other way around  I can make a parent class with abstract methods or even better: provide an Interface that my form implements. ---------------------------------------------------------------------- HeavyUser · 2016-05-09 12:32 Quote from: Turbonobo on May 09, 2016, 02:21:46 pmQuote from: HeavyUser on May 08, 2016, 11:02:11 pmNo. Well almost you could write a small IDE addin to help you convert a number of selected components to code and copy it in memory allowing you to place it where ever you need. Then you delete the components from the designer and move their declaration in to private just before releasing a stable version and you have (kind of) both worlds. It smells like c/c++ but hey it works. I was considering this... but I was hoping someone knew a better solution. Gexperts has a tool that does this for Delphi http://www.gexperts.org/tour/index.html?components_to_code.html But I think I found a solution: Quote from: HeavyUser on May 08, 2016, 11:02:11 pmNo. You can not lower the visibility of an ancestor class sorry. But I can do it the other way around  I can make a parent class with abstract methods or even better: provide an Interface that my form implements. Yes you can and its my preferred way for a number of things, I just don't see any designer support in this solution. ---------------------------------------------------------------------- Graeme · 2016-05-10 14:45 Quote from: Turbonobo on May 08, 2016, 12:44:24 pmIf I want to restrict the access to components on a form. (for encapsulation and code sanity). I could declare them as 'private', A very good idea! Quote but then I wouldn't be able to use the form designer (no RTTI for private members, makes sense). Yes, a limitation of any form designer that relies purely on RTTI (ie: published properties). That includes Lazarus, Delphi and MSEide. :-( QuoteIs there a workaround for this? (besides doing everything in code?) Could I for example use the form designer on a base class and make the components 'private'  or hidden in a derived class? No you can't reduce visibility after the fact. You can only increase visibility (eg: private → protected → public → published.). Anyway, many years ago I had the exact same thought as you. I design normal classes (objects) with as much reduced visibility as possible, and only surface properties when needed. But why does Forms (which are also class instances) not adhere to that. So I made a plan. fpGUI's form designer allows you to define your component at any level, but defaults to private. fpGUI's Form Designer is a stand-alone tool, so works outside the scope of any IDE. Currently it is obviously catered towards fpGUI's widgets, but it should take very little effort to adapt it to LCL components too. The other benefits of fpGUI's Form Designer are: it only uses .PAS files, no *.lfm or *.dfm files - so much easier search and replace of components or properties.The Object Inspector can handle properties and even components it doesn't know about - via pure coding magic. ;-)You can also design multiple forms inside a single .PAS file - not possible in Delphi, Lazarus or MSEide. Here is a [rather old] screenshot of the Visual Forms Designer - I really should add a new screenshot in there.   http://fpgui.sourceforge.net/screenshots_apps.shtml Here is an animated GIF show an old version of Visual Forms Designer in action.   http://geldenhuys.co.uk/~graemeg/editform.gif and here is the .PAS file that video created.   http://geldenhuys.co.uk/~graemeg/editform.pas I also integrated the Visual Forms Designer inside Lazarus and MSEide via their “External Tools” functionality, so I can launch the forms designer against the current open file simply via a keyboard shortcut.