Delphi Deep Exploration-Database-Clear ActiveX Control
The powerful database development function is undoubtedly the crown jewel of Delphi. At least 80% of people use Delphi to develop database programs every day. Based on the building block VCL database control, as well as the powerful third-party database clear controls provided by companies such as Dev ExPRess and Woll2Woll, it is no exaggeration to say that we only need to simply set the properties and drag the mouse a few times to complete the database very quickly. Program development. Because Delphi's database function is too powerful, many people overlook the fact that ActiveX can also provide flexible database binding. Unfortunately, none of the several third-party ActiveX controls provided by Delphi itself is database-savvy. control, which indirectly led to a misunderstanding, causing many people to mistakenly believe that Delphi does not support ActiveX controls with clear data (including me a long time ago). This is of course a wrong view. Using the data-clear ActiveX control in Delphi In the following example, we will use the Microsoft Calendar 9.0 control installed in the system with Office 2000 for demonstration. It is a data-clear control. Select Component | Import ActiveX Control, select Microsoft Calendar 9.0 (see Figure 1.12 below), change the class name from TCalendar to TAXCalendar to avoid conflict with the original control of the same name in the system, and then click the Install button to install this control.
Create a new program and place a TAXCalendar control on the form. Put another DataSource and Table control. Set the Dataset of DataSource to Table1, then set the DataBaseName property of Table1 to DBDEMOS, the TableName property to EMPLOYEE.DB, and finally set the Table1.Active property to True. Next, it’s time to bind the database. Note that the introduced TAXCalendar has two properties, DataSource and DataBindings, which are different from the general ActiveX control. This is because the ActiveX Import Wizard first determines whether it is a database-friendly control before introducing the ActiveX control. If If so, use TDBOleControl as the base class to inherit. If not, inherit from TOleControl. The TDBOleControl implements the DataBindings and DataSource properties. Set the AXCalendar1.DataSource property to Datasource1. At this time, if we right-click the TAXCalendar control on the form, we will find an additional DataBindings... menu item in the right-click menu (see Figure 1.13 below).
After clicking the menu item, the data binding attribute edit box will be displayed (see Figure 1.14 below). Select HireDate in the FieldName list box, select Value(12) in the Property Name list box (12 is the Dispid number of Value), and click the Bind button to establish a data association between the database field and the Value property.
Finally, place a DBGrid and DBNavigator control on the form, and set their DataSource to DataSource1. When you run the program and move the current data position, you can notice that the date display in ActiveX will also change accordingly, consistent with the Hiredate in the database. As shown in Figure 1.15:
Creating Data-Clear ActiveX Controls Although we already know that Delphi can indeed use data-clear ActiveX controls, a new question arises. Can Delphi itself create data-clear ActiveX controls? Since Delphi provides one-step conversion and generation of ActiveX controls, many people hope to convert the powerful database controls in Delphi into ActiveX controls so that they can still enjoy the same experience as in Delphi when developing database programs in other development environments that support ActiveX. The same relaxed and happy feeling. But as I mentioned in the first part, because the internal mechanism of Delphi's data-clear control is too different from that of ActiveX database control, Delphi cannot simply directly convert its powerful database control. Does that mean there is nothing we can do? of course not! In fact, using the type library editor, we can implement an ActiveX control with clear data very easily. Let us use the TEdit control to test it and see how to do it. Select the menu command New | ActiveX | ActiveX Control to start the ActiveX Control Convert Wizard, select the TEdit control as the conversion object, and generate the EditX ActiveX framework. Next, we will transform TEdit's Text property to support data binding. Select View | Type Library to view the type library generated by Delphi, select the Text property, and then switch to the Flags property page (see Figure 1.16 below).
Note that there are many check boxes on the Flags property page. For us, we only care about the options related to data binding. They are Bindable, Display Bindable, Default Bindable, Immediate Bindable and Request Edit options. After marking an attribute as bindable and binding it to a database field, when the user modifies the attribute, the control will notify the database that the value has changed and request the database to record the update status. The database will in turn notify whether the record update was successful. . The Bindable option indicates that the property supports data binding. If the property is marked as bindable, the property will notify its container when its value changes. Request Edit indicates that the property supports OnRequestEdit notification messages, which allows the control to ask the container whether the property value is allowed to be modified by the user. Display Bindable means that the container can show the user that this property is bindable. Default Bindable means that it is the only, default bindable attribute. To use it, the Bindable attribute must be marked at the same time. When Immediate Bindable is marked, all changes will be notified, and the bindable and Request Edit flags need to be set. Next we mark the Text property of the EditX control as Bindable, Display Bindable, Default Bindable, and Request Edit. Then click the Refresh button to refresh the type library, and finally select Run | Register ActiveX Server to register the ActiveX control. If you introduce the new EditX ActiveX control, you will find that it does implement the data binding function. The following is a diagram of the operation of an EditX program that uses data clearing. (The implementation of Delphi's ActiveX data clearing function is indeed simple and cool. , isn’t it?