Use of Tframe components in Delphi
Starting with Delphi 5.0, Borland introduced a new visual container class Tframe. This class, which we call the Tframe framework component, enables program developers to visualize a set of components that are then reused in the system.
The TFRAME framework has two main benefits:
First, the framework can significantly reduce the amount of resources that need to be stored in the project.
Second, the framework allows you to visualize the creation of objects that can be copied and extended. For visual form Inheritance (VFI), you can enjoy the same benefits.
VFI allows you to easily create an inherited form. the VFI limit is that you must use the form in a "All or nothing" way. More specifically, when you use VFI, you always create a new form.
The Tframe framework can also be thought of with component templates (with one or more components and component | The Create Component template command is saved to a group in the palette) similar. However, similarities are limited to those that are visually designed (unlike traditional completely code-based component designs). In fact, the difference between a framework and a component template is huge. As we have learned, the framework is an instance of a defined class, and the framework changes as the class changes. In contrast, a component template is a collection of components. Changes to the component template do not affect objects that were previously created from this template.
Create a Frame
1> Select File | New application to build a project.
2> Select File | New frame creates a tframe framework. On this frame, add three labels and three dbedit, plus a dbnavigator and a datasource. Set the label's caption to Id,first name and last name, respectively. Set the DataSource property of Dbedit and Dbnavigator to DataSource1.
3> sets the Name property of the framework to nameframe. (It is important for the framework to have a meaningful name compared to other objects) Finally, select File | Save as saves the frame. Here, save the frame with the file name Namefram.pas.
Working with Frames
1> Select Form1 in the project established by the above steps.
2> Add two GroupBox to the form, one on top of the other. Set the first caption to customers, and the second caption to Employees.
3> now add a frame. Select the standard page of the palette, click the frame component and drag it to the GroupBox named Customers. At this point, Delphi displays a select frame to Insert dialog box.
4> Select Nameframe in the dialog box. The framework is now displayed in the GroupBox named Customers. Repeat this step, this time placing the framework in the GroupBox named employees. You may want to adjust the size of the frame, which is related to how you initially placed it.
5> put two table components on the form and set their DatabaseName property to Iblocal. Set the TableName property of Table1 to CUSTOMER and set the TableName property of Table2 to employee. Set the active property of the two table to true to make them valid.
6> the steps below will make things interesting. Select DataSource in GroupBox named Customers and set its DataSet property to Table1. In general, you cannot directly select an object in a component, but the framework is an exception. You can select any object in the frame to manipulate its properties. Then, select DataSource in GroupBox named Customers, whose DataSet property is set to Table2.
7> Finally, set all the dbedits. The DataField properties of the three dbedits in GroupBox named Customers are set to Cust_no,contact_first and Contact_last, respectively. For employees, set the DataField property to Emp_no,first_name and Last_Name.
8> Save the project and run it.
Framework and inheritance
1> in the project created above, press [SHIFT][F12] and select Nameframe in the list of forms.
2> set DataSource's Autoedit property to False.
3> Then, select Dbnavigator, expand its Visiblebuttons property, and set the Nbinsert,nbdelete,nbedit,nbpost and Nbcancel flags to false.
4> now look at your main form and notice that the descendants of the two nameframe inherit the changes you made to the frame.
Overloading the properties of a containing component
One of the advantages of the framework (as with VFI) is that you can change the properties and event handlers of objects in the framework. These modifications overload the inherited values. More specifically, subsequent modifications to the overloaded properties of the initial framework will not alter the values of the overloaded properties of descendants.
1> in the GroupBox named Customers, select the label titled "ID" and change its Caption property to "Customer No:" In Object Inspector. Now select the label titled "ID" in GroupBox named Customers, whose caption attribute is changed to "Employee ID:".
2> Press [SHIFT][F12] and select Nameframe. Change the Caption property of the label titled "ID" to identifier.
3> back to the main form, note that the Caption property of the label does not change to identifier. They still maintain their overloaded values.
4> This effect is implemented by the information stored in the DFM file.
Notice that all of the objects in the frame whose properties have been changed appear in the built-in part of the DFM file. However, this section lists only those values that have changed, and all other property values are either set by the value of the initial frame (they are already stored in the framework's DFM file), or the values are defined by the default class for each component.
just as an event handler for an object in a form is a public method of a form class, the object's event handler function in the framework is also the public method of the framework class. ( The code snippet does not actually say that these methods of the framework are public, but that they are declared in the default visible part of the framework Declaration, whose visibility is exposed by default.) )
Framework saves Resources
There are two frames in the Declaration form. We have discussed the Twobuttonframe framework. The second frame displays the company logo, named Logoframe.
Logoframe appears in multiple forms in Framdemo. An alternative to using the framework is to set an Image object in each form to display the company logo. However, using a framework can significantly reduce the resources that need to be compiled into an EXE file, generating a smaller executable file.
If you place a timage on the form, the DFM file for the form will have to contain the binary number representation of the entire company logo. Also, each form that contains an image repeats the resource. However, when using a framework, resources are defined only once.
Simplifying the use of frameworks
In a single, small project, it is fairly straightforward to use the framework components on the Palette standard page. But for larger, or to use the same framework in multiple programs, you need to make the operation simpler. Fortunately,Delphi allows you to put a single frame into the palette so that these frameworks do not require additional steps and are simply reused. The framework can also be placed in object Repository, making it simple to copy. Both of these techniques are described below.
Adding frames to the palette
by adding a framework to a component template, you can make placing this framework as simple as other components. In contrast, using the framework component in the standard page of the component template requires four steps, and restricts you to placing only the frames that you have defined in your project. adding a specific framework to a component template requires:
1> Save the frame. If you want to use a framework in multiple programs, I highly recommend that you save the framework to a directory that will not be deleted when you upgrade Delphi. For example, create a directory named C:\Program Files\borland\delphiframes to save your frame.
2> Select the frame and right-click on the button above, select Add to Palette,delphi the Component Template Information dialog box is displayed.
3> define the name of the framework component in the Component Name column, and in the Palette page column, fill in the page you want the framework to appear within the component template. If you have created a 24-pixel, 16-color icon for the frame, click the Change button to select the BMP file. When you're done, click the OK button.
Working with frames from component templates
To use a frame that has been added to the palette, select the page in the palette where you saved the frame, select the frame icon, and drag the frame out of the form you want the frame descendants to appear on. This process takes only two steps.
To add a framework to an object library
By adding the framework to the object library, you can make it easier to copy the framework into a new project. It is especially important that the object library provides the ability to place the inherited framework into the new project, which keeps the relationship between the framework and its ancestors. To add a framework to an object library, you need the following steps:
1> Save the frame. In addition to saving the framework to the Delphi Objrepos directory or the shared directory, you can also use the directory where you saved the framework that you added to the component template. If you use a shared library, saving the framework to a shared directory is particularly good, and it allows multiple developers to share the framework.
2> the right mouse button on the frame, select Add to Repository,delphi will display the Add to Repository dialog box.
3> just like you add other templates to object repository, fill in the Add to Repository dialog box. Click the OK button when you are done.
Using frames from the object library
Follow the steps below to use the framework from the object library:
1> Select File | New.
2> Select the page where you saved the frame template in Object repository.
3> Select the icon that represents the frame, and then select the Inherit radio button.
4> Click the OK button to add an inherited version of the frame to your project
If you choose the Copy radio button instead of the Inherit radio button, the added frame will be a copy of the original frame. It is useful when you need to create a new framework, but do not want to maintain its relationship with the original framework.
Do you have a different effect on adding a framework that needs to be reused to a component template versus adding it to the object library? The answer is: "yes!". In most cases, you will add the frames you use frequently to the palette. So when you place the frame, you get an instance of the frame. You can then easily modify the properties and event handlers of this instance , as described earlier in this article. in contrast, placing a frame from the object library results in a new class, not an instance. This class is either the original copy or the original inheritance , depending on which radio button you selected in the Object Repository dialog box. If you want to use the framework in your project, adding a framework instance is more meaningful than defining a new class of frames. If this is the case, saving the framework to the palette is the best approach.
There is a situation where you want to use object libraries: When you need to create a frame hierarchy specifically, and each frame descendant introduces an additional object, method, or event handler function. At this point, the inheritance provided by the object library makes it easier to create descendants. define the descendants that need to use the framework frequently, and you can add them to the palette to simplify their use.
The use of Delphi framework frames