RAD Studio |  |

Making a control data aware
Collapse All
Topics

Name | Description
Adding the Data Link | The connection between a control and a database is handled by a class called a data link. The data link class that connects a control with a single field in a database is TFieldDataLink. There are also data links for entire tables. A data-aware control owns its data link class. That is, the control has the responsibility for constructing and destroying the data link. For details on management of owned classes, see Creating a graphic control
Adding the ReadOnly property | By adding a ReadOnly property, you will provide a way to make the control read-only at design time. When that property is set to True, you can make all cells in the control unable to be selected.
Allowing Needed Updates | The read-only calendar uses the SelectCell method for all kinds of changes, including setting the Row and Col properties. The UpdateCalendar method sets Row and Col every time the date changes, but because SelectCell disallows changes, the selection remains in place, even though the date changes. To get around this absolute prohibition on changes, you can add an internal Boolean flag to the calendar, and permit changes when that flag is set to True:
Changing the Default Value of FReadOnly | Because this is a data editing control, the ReadOnly property should be set to False by default. To make the ReadOnly property False, change the value of FReadOnly in the constructor:
Creating a Data Browsing Control | Creating a data-aware calendar control, whether it is a read-only control or one in which the user can change the underlying data in the dataset, involves the following steps: Creating and registering the component. Adding the data link. Responding to data changes.
Creating a Data Editing Control | When you create a data editing control, you create and register the component and add the data link just as you do for a data browsing control. You also respond to data changes in the underlying field in a similar manner, but you must handle a few more issues. For example, you probably want your control to respond to both key and mouse events. Your control must respond when the user changes the contents of the control. When the user exits the control, you want the changes made in the control to be reflected in the dataset. The data editing... more
Creating and registering the data-aware component | You create every component the same way: create a unit, derive a component class, register it, compile it, and install it on the Tool palette. This process is outlined in Creating a new component. For this example, follow the general procedure for creating a component, with these specifics: Call the component's unit DBCal. Derive a new component class called TDBCalendar, descended from the component TSampleCalendar. The section Customizing a grid shows you how to create the TSampleCalendar component. Register TDBCalendar on the Samples page of the Tool palette. The resulting unit descending from TCustomGrid in a... more
Declaring the Access Properties for a Data-aware Control | Every data-aware control has a DataSource property that specifies which data source class in the application provides the data to the control. In addition, a control that accesses a single field needs a DataField property to specify that field in the data source. Unlike the access properties for the owned classes in the example in Creating a graphic control these access properties do not provide access to the owned classes themselves, but rather to corresponding properties in the owned class. That is, you will create properties that enable the control and its data link to share the same data source... more
Declaring the Class Field | A component needs a field for each of its owned classes, as explained in Declaring the class fields. In this case, the calendar needs a field of type TFieldDataLink for its data link. Declare a field for the data link in the calendar:
Handling Mouse-down and Key-down Messages | When the user of the control begins interacting with it, the control receives either mouse-down messages (WM_LBUTTONDOWN, WM_MBUTTONDOWN, or WM_RBUTTONDOWN) or a key-down message (WM_KEYDOWN) from Windows. To enable a control to respond to these messages, you must write handlers that respond to these messages. Responding to mouse-down messages. Responding to key-down messages.
Initializing the Data Link | A data-aware control needs access to its data link throughout its existence, so it must construct the data link object as part of its own constructor, and destroy the data link object before it is itself destroyed. Override the Create and Destroy methods of the calendar to construct and destroy the datalink object, respectively:
Making a Control Data Aware | When working with database connections, it is often convenient to have controls that are data aware. That is, the application can establish a link between the control and some part of a database. Delphi includes data-aware labels, edit boxes, list boxes, combo boxes, lookup controls, and grids. You can also make your own controls data aware. For more information about using data-aware controls, see Using data controls . There are several degrees of data awareness. The simplest is read-only data awareness, or data browsing, the ability to reflect the current state of a database. More complicated is editable... more
Making the Control Read-only | Because this data calendar will be read-only with respect to the data, it makes sense to make the control itself read-only, so users will not make changes within the control and expect them to be reflected in the database. Making the calendar read-only involves: Adding the ReadOnly property. Allowing needed updates. Note: Note that if you started with the TCalendar component from Delphi's Samples page instead of TSampleCalendar, it already has a ReadOnly property, so you can skip these steps.
Modifying the Change Method | The Change method of the TDBCalendar is called whenever a new date value is set. Change calls the OnChange event handler, if one exists. The component user can write code in the OnChange event handler to respond to changes in the date. When the calendar date changes, the underlying dataset should be notified that a change has occurred. You can do that by overriding the Change method and adding one more line of code.
Responding to Data Changes | Once a control has a data link and properties to specify the data source and data field, it needs to respond to changes in the data in that field, either because of a move to a different record or because of a change made to that field. Data link classes all have events named OnDataChange. When the data source indicates a change in its data, the data link object calls any event handler attached to its OnDataChange event. To update a control in response to data changes, attach a handler to the data link's OnDataChange event. In this case,... more
Responding to Key-down Messages | A KeyDown method is a protected method for a control's OnKeyDown event. The control itself calls KeyDown in response to a Windows key-down message. When overriding the inherited KeyDown method, you can include code that provides other responses in addition to calling the OnKeyDown event.
Responding to Mouse-down Messages | A MouseDown method is a protected method for a control's OnMouseDown event. The control itself calls MouseDown in response to a Windows mouse-down message. When you override the inherited MouseDown method, you can include code that provides other responses in addition to calling the OnMouseDown event. To override MouseDown, add the MouseDown method to the TDBCalendar class:
Updating the Dataset | So far, a change within the data-aware control has changed values in the field data link class. The final step in creating a data editing control is to update the dataset with the new value. This should happen after the person changing the value in the data-aware control exits the control by clicking outside the control or pressing the Tab key. Note: VCL applications define message control IDs for operations on controls. For example, the CM_EXIT message is sent to the control when the user exits the control. You can write message handlers that respond to the message. In this... more
Updating the Field Data Link Class | There are two types of data changes: A change in a field value that must be reflected in the data-aware control. A change in the data-aware control that must be reflected in the field value. The TDBCalendar component already has a DataChange method that handles a change in the field's value in the dataset by assigning that value to the CalendarDate property. The DataChange method is the handler for the OnDataChange event. So the calendar component can handle the first type of data change. Similarly, the field data link class also has an OnUpdateData event that occurs as the user... more

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!