③ decide what to store
Users can also control whether Delphi stores every attribute of a part. By default, all properties declared in the published portion of an object are stored. However, you can choose not to store the given attributes or to design a function to decide whether to store properties at run time.
The way to control whether Delphi stores properties is to add a stored instruction after the property declaration, followed by true or false, or a Boolean method name. You can add a stored expression to any attribute's declaration or re-declaration. The following code shows three new properties of the part declaration. A property is always to be stored, one is not saved, and the third is determined by the value of the Boolean method:
Type
Tsamplecompiment = Class (Tcomponent)
Protected
function Storeit:boolean;
Public {does not save under normal conditions}
Property Important:integer stored True; {Always store}
Published {Normally Save}
Property Unimportant:integer stored False; {Do not save}
Property Sometimes:integer stored Storeit; {storage depends on function value}
End
Initialization after ④ loading
After the part reads all the attributes from the stored description, it invokes a virtual method named Loaded, which provides an opportunity to perform any initialization as needed. Calling loaded is before the form and its control is displayed, so there is no need to worry that initialization will bring the screen flicker.
Initializes the part when it loads the property, overriding the loaded method.
The first thing to do in the loaded method is to invoke the inherited loaded method. This allows any inherited properties to be initialized before your part performs initialization.
The following code comes from the tdatabase part. After loading, Tdatabase attempts to reconstruct the connection that was opened when it was stored, and describes how to handle the connection when an exception occurs.
Procedure tdatabase.loaded
Begin
Inherited Loaded; {Inherited methods are always called First}
Modified; {Set Internal flag}
Try
If fstreamedconnected then Open; {Rebuilding joins}
Except
If csdesigning in Componentstate then {at design time}
Application.handleexception (self) {Let Delphi handle exceptions}
else raise; otherwise
End
End
19.3 Delphi Parts Programming Example
19.3.1 CREATE database-related Calendar control-tdbcalendar
When working with database joins, it is important to directly associate control and data. That is, an application can establish a chain between control and the database. Delphi includes data-related labels, edit boxes, list boxes and grids. Users can make their own control relevant to the data.
There are several levels of data correlation. The simplest is read-only data-related or data-browsing, and the ability to reflect the current state of the database. The more complex is data-related editing, which means that the user can manipulate the data in the database on control.
The simplest scenario in this section is to create read-only control of a single field that joins the database. In this example, the Tcalendar part in the Samples page of component palette is used.
Creating data-related calendar controls includes the following steps:
Create and register a part
Make control read-only
Add Data Link
Responding to data changes
19.3.1 1 Creating and registering parts
The creation of each part starts in the same way, and in this case the following procedures are followed:
Name the part library unit dbcal
Inherits a new part from Tcalendar, named Tdbcalendar
Register in the Samples page of component palette Tdbcalendar
The following is the code you created:
Unit dbcal;
Interface
Uses Sysutils, Wintypes, WinProc, Messages, Classes, Graphics, Controls,
Forms, Grids, Calendar;
Type
Tdbcalendar=class (Tcalendar)
End
Procedure Register;
Implementation
Procedure Register;
Begin
Registercomponents (Samples,[tdbabendar]);
End
End.
19.3.1.2 make control read-only
Because this data calendar responds to data in a read-only manner, users cannot change the data in control and expect them to be reflected in the database.
Make the calendar read-only contains the following two steps:
Adding read-only properties
Allow Required Updates