29.09.2021, Vladimír Klaus, navštíveno 1492x
Quite often I create new forms, but they are very similar to existing ones. I've always done it the old fashioned way - i.e. saving the existing one under a new/different name, editing that copy, etc. Unfortunately, I never thought of using Object Repository, which would save a lot of time and work. I haven't used it in other situations either, let alone with my own objects. And let's be honest - one of the reasons is a bit of a strange control.
Incidentally, that's also why this article was written, which simply describes the basic procedures when you want to create a dialog box template with two buttons, an icon and pre-made text, for example, and then use it easily, but maybe even change it.
Creating a form
Create a new project (it's just a formality) and then create the form normally, including other components, methods, events, uses, etc. Just save it somewhere outside the project, because Object Repository still refers to the source files, so if you delete the project, you will lose the template.
The second reason is backup, see below. I have a set of my own libraries in d:\PasVK2020\, so I created a subfolder d:\PasVK2020\Repository\.
Also, think about the name of the form, as you may want to use inheritance later, so that it makes sense. I just called it VKDialog01Form.
Adding to Object Repository
To add a form to the Repository, you need to right-click on it and select "Add to Repository...".
A dialog will appear, which you can simply fill in, or you can create your own category in which the template will be placed. Not physically, as I mentioned above.
Using a form template
To use a template, you must select it using the menu:
File > New > Other
The bottom option, which allows you to choose different uses of the template, is essential:
- Copy - Creates a copy that has nothing further to do with the template
- Inherit - A new form is created, but based on the template. When you change the template, this one will change as well. Of course, this only applies to the parts you haven't changed in the successor. In short, classic inheritance.
- Use - The form is added to the project, which is a bit dangerous. You forget it's a template and start changing it...
I prefer "Copy" because for simple forms, like in this example, I won't be using some ancestor cleverness, but will be changing it almost completely. And it's also good in situations where there is some cleverness in the template that you might want to get rid of in the copy.
Modifying the template
As mentioned, the whole system works on the fact that it still references the original files. Editing therefore consists of directly editing the files in question. And this can also be done, for example, by using the "Use" mentioned above.
Changing and removing templates
By modify or delete I mean the template parameters and the link to it, not the source files! Use menu:
Tools > Template Libraries...
And then "Properties...". In this dialog you will already find buttons to change/remove.
Backup and restore after installing new Delphi
The backup needs to be divided into two parts. The first is to backup the source files directly, which should not be a problem. But how to backup the location information in the Object Repository? It's hard to say. You can find a number of files in this folder, the most hopeful is directly in Repository.xml...
c:\Users\{USERNAME}\AppData\Roaming\Embarcadero\BDS\21.0\
But the question is whether it is appropriate to replace this file with the old one in the new Delphi. Personally, I don't worry about this at all, and after the installation I will manually add the few templates again.
Resources: