Name |
Description |
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 | |
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. | |
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: | |
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-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:
| |
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 | |
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:
| |
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 | |
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: | |
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.
| |
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: | |
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 | |
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: 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. | |
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. | |
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 | |
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. | |
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: | |
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 | |
There are two types of data changes:
Similarly, the field data link class also has an OnUpdateData event that occurs as the user... more |
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|