Multi-tier Database Development 12: Using Data Controls

Source: Internet
Author: User
Chapter 4 Use Data Controls
Data Controls are frequently used in database applications. Different from the preceding dataset components such as tTable, tquery, tstoredproc, and tclientdataset, the Data Control is visualized. That is to say, if the attributes of these components are modified, they can be immediately reflected in the form. If the enabled attribute of these components is set to true and the active attribute of the dataset is set to true, you can see the data during the design period.
12.1 Data Controls in Delphi 4
In the IDE of Delphi 4, data controls are all located on the "Data Controls" page of the component palette. The following describes the data controls:
. TDBGrid displays data in a grid consisting of rows and columns.
. Tdbnavigator is used to navigate the records of the entire database, such as turning one record forward, one record backward, the first record, and the last record.
. Tdbtext displays the field content as a tag.
. Tdbedit displays the content of a field in the editing box.
. Tdbmemo displays the content of the remarks field in the form of a multi-line text editor.
. Tdbimage is used to display image fields in the database.
. Tdblistbox displays data in the form of a list box.
. Tdbcombobox displays data in the form of a combo box.
. Tdbcheckbox displays data in the form of check boxes.
. Tdbradiogroup displays data in the form of a single-region grouping box.
. Tdblookuplistbox displays the data of the lookup table in the form of a list box.
. Tdblookupcombobox displays the data of the lookup table in the form of a combo box.
. Tdbrichedit displays the content of the remarks field in the RTF format.
. Tdbctrlgrid is similar to TDBGrid, but each unit can set properties separately.
. Tdbchart displays database data in charts. Its usage is similar to that of tchart.
The above data control features data awareness during the design period. If the datasource attribute is correctly set and a data source is specified, the data can be seen immediately without the need to compile and run the program.
12.2 basic usage of Data Controls
This section describes the basic usage of data controls, including how to specify a data source, how to edit and update data, how to disable and allow data refresh.
12.2.1 specify a data source
The Data Control must connect to the dataset through the tdatasource component. The role played by the tdatasource component is actually a bridge between the data control and the dataset.
First, place a dataset component on a form or data module, set its databasename attribute to specify the database to be accessed, and set its tablename attribute to specify the table to be accessed.
Next, place a tdatasource component on the form or data module and set its dataset attribute to specify the dataset.
Then, place a Data Control on the form and set its datasource attribute to specify the tdatasource component. The dataset attribute of the tdatasource component already specifies a dataset.
Finally, set the datafield attribute of the Data Control to specify the field to be displayed. However, for the TDBGrid, tdbctrlgrid, and tdbnavigator components, you do not need to set the datafield attribute because these controls work on the entire dataset.
12.2.2 edit and update data
Besides the tdbnavigator component, other data controls are used to display and edit data. This section describes how to edit data.
To enable users to edit data, the dataset must enter the dsedit state. If the autoedit attribute of tdatasource is set to false, you cannot directly edit the data unless the program calls the Edit function.
To enable users to modify data in the data control, you must set the readonly attribute of the Data Control to false. If the readonly attribute is set to true, the data displayed in the Data Control is read-only.
Generally, the enabled attribute of the tdatasource component is set to true. If this attribute is set to false, the Data Control cannot display or modify data.
If the readonly attribute of the dataset component is set to true, the dataset is read-only. modifications made in the data control cannot be written to the dataset.
In addition to the TDBGrid component, when you modify the value of a field, you also need to remove the input focus to write new data to the dataset. You can press ESC to cancel the modification at any time before removing the input focus.
In the Grid created by the TDBGrid component, when you modify the value of a field, you also need to move the input focus to another record to write new data to the dataset.
12.2.3 prohibit and allow data refresh
When the program is traversing the entire dataset or searching for a specific record, the data control should be temporarily disabled to refresh the data, which can speed up the traversal or searching and prevent the screen from flashing.
Call the disablecontrols function of a dataset to temporarily disable the data control that connects to the dataset to refresh the data. The disablecontrols function is usually called before cyclic operations. After the loop ends, the program should immediately call the enablecontrols function of the dataset component to allow data refresh.
We recommend that you use the try... Finally structure to ensure that the Refresh can always be restored at the end. In this way, even if an exception occurs in the loop, enablecontrols can always be called.
The following code demonstrates how to call the disablecontrols and enablecontrols functions:
Custtable. disablecontrols;
Try
Custtable. first;
While not custtable. EOF do
Begin
...
Custtable. Next;
End;
Finally
Custtable. enablecontrols;
End;
12.2.4 manual data refresh
Calling the refresh of a dataset can read the latest data in the dataset and refresh the data control. This function is particularly useful in multi-user environments because other users may have changed the data in the dataset.
Sometimes calling refresh may lead to unexpected results. For example, if another user has deleted a record, after calling refresh, the record will disappear from the data control.
12.3 data control for displaying a single Field
Some data controls take one or more fields of the database as the work content, such as tdbtext and tdbedit, while some data controls take the entire data set as the work content, such as TDBGrid and tdbnavigator.
The data control that displays a single field is often evolved from a standard Windows Control. For example, the tdbedit component can be considered a data-aware version of tedit.
Figure 12.1 Use the tdbtext component to display data as labels
12.3.1 display data as tags
The tdbtext component is a read-only data control, which is very similar to the tlabel component and the tstatictext component. The tdbtext component can display data as labels to mark other controls. For example, you can use a tdbtext component to display the fish name (common_name field) under the fish image, as shown in Figure 12.1.
The tdbtext component must specify a field. When you use a navigator or other means to browse records, the data displayed by the tdbtext component automatically changes because the tdbtext component always displays the data of the current record. For example, in Figure 12.1, When you click the arrow at both ends of the raster's tumble bar with the mouse, the fish image automatically changes and the fish name changes accordingly, all of this requires no programming.
The autosize attribute of the tdbtext component is generally set to true, because the content length of the field may be different. If the autosize attribute is set to false, some long content may be truncated.
12.3.2 display and edit data
The tdbtext component can only display data and cannot edit data. To display and edit data, you must use the tdbedit component. Tdbedit can be considered as the data-aware version of tedit.
For example, a tdatasource component is called mermerssource, and its dataset attribute points to a tTable component called customerstable. Put a tdbedit component on the form, set its datasource attribute to customerssource, and set its datafield attribute to custno. The tdbedit component can immediately display the value of the custno field. You can enter a new value in the edit box.
12.3.3 display and edit multi-line text
The tdbmemo component is a data-aware version of The tmemo component. It displays the content of the remarks field in the dbase and paradox databases.
Different from tdbedit, tdbmemo can display text in multiple lines, and also allow users to type multiple lines of text.
By default, tdbmemo allows you to modify the text displayed by tdbmemo. If you do not want the user to modify the text, set the readonly attribute to true. To allow users to insert a tab in the text, set the wanttabs attribute to true. Otherwise, when the user presses the tab key, the input focus is removed, rather than the tab is inserted.
To limit the maximum number of characters that a user can enter, you can set the maxlength attribute. If this attribute is set to 0, there is no limit.
In addition, the scrollbars attribute can be set to whether to add a scroll bar, the wordwrap attribute can be set to whether to allow automatic detour, And the alignment attribute can set the text alignment.
During the runtime, you can call the cuttoclipboard and copytoclipboard functions to cut and copy the selected text to the clipboard. Call pastefromclipboard to paste the text in the clipboard. If the autodisplay attribute is set to true, the tdbmemo component will automatically refresh when the content of the field specified by the datafield attribute changes. If the autodisplay attribute is set to false, only the field name is displayed on the tdbmemo component. You must double-click this component or the program calls loadmemo to refresh the data.
12.3.4 display text in RTF Format
The tdbrichedit component can be considered as the data-aware version of The trichedit component. It is used to display formatted text in BLOB fields in the RTF format. It is similar to the tdbmemo component and can display multiple lines of text.
Note: although the tdbrichedit component can display text in the RTF format and provides strong editing functions, it does not provide a user interface and the application must design a corresponding user interface, the powerful functions of the tdbrichedit component can be used out.
By default, the tdbrichedit component allows users to type new text. If you do not want the user to modify the text, you can set the readonly attribute to true.
To allow users to insert a tab in the text, set the wanttabs attribute to true. Otherwise, when the user presses the tab key, the input focus is removed, rather than the tab is inserted.
To limit the maximum number of characters that a user can enter, you can set the maxlength attribute. If this attribute is set to 0, there is no limit.
If the autodisplay attribute is set to true, when the content of the field specified by the datafield attribute changes, the tdbrichedit component will automatically refresh. If the autodisplay attribute is set to false, only the field name is displayed on the tdbrichedit component. You must double-click the component or program to call the loadmemo function to refresh the data.
12.3.5 display and edit Images
The tdbimage component can be considered as the data-aware version of The timage component. It can display the content of the Blob field. After the tdbimage component retrieves the image from the dataset, it creates a copy locally in the dib format.
You can call the cuttoclipboard or copytoclipboard function to cut or copy the image to the clipboard. Call pastefromclipboard to paste the image from the clipboard.
If the stretch attribute is set to true, the image is automatically scaled to adapt to the size of the tdbimage component, which may cause image deformation. If the autodisplay attribute is set to true, the tdbimage component will automatically refresh when the content of the field specified by the datafield attribute changes. If this attribute is set to false, only the field name is displayed on the tdbimage component. You must double-click this component to refresh the data. Of course, you can also call loadpicture to refresh the data.
12.4 display and edit data with a list box and a combo box
There are four special data controls that can be used to display and edit data using the list box and combo box. They can be considered as the data-aware version of the standard list box and combo box. The following describes the four data controls:
. Tdblistbox displays a set of data in a list box, allowing you to select a value from it.
. Tdbcombobox displays a group of data with a combo box, allowing you to select a value from it.
. Tdblookuplistbox uses a list box to display a group of data in another dataset, allowing you to select a value from it.
. Tdblookupcombobox: Use a combo box to display a group of data in another dataset, allowing you to select a value from it.
12.4.1 tdblistbox
The tdblistbox component can display a set of data in a list box, from which you can select a data. When a user browses a record, the program will automatically search for items that match the field value in the list box. If this item is found, select this item, as shown in Figure 12.2.
Select matching items in the list box. When you select an item in the list box, the program automatically changes the value of this field in the current record to the value selected in the list box. Of course, the post function must be called to make the modification effective.
To set items displayed in the list box during the design period, click the ellipsis next to the items attribute to open a string List editor and enter some strings.
If the integralheight attribute is set to true, the height of the list box is always an integer multiple of the height of the item.
12.4.2 tdbcombobox
The tdbcombobox component is actually the data-aware version of The tcombobox component. It can display a group of data in the form of a combo box, allowing users to select a value from the list or directly enter a value.
The items attribute is used to set a group of data to be displayed in the list. During the design period, you can click the ellipsis next to the items attribute to open a string List editor and enter some strings.
The dropdowncount attribute is used to set the number of items that can be displayed without adding a scroll bar when the user pulls down the combo box. The default value is 8, if the number of items in the drop-down box exceeds 8, the scroll bar is added. If the actual number of items does not have more than the value specified by the dropdowncount attribute, the height of the drop-down combo box is automatically reduced.
When the style attribute is set to csownerdrawfixed, The itemheight attribute is used to set the height of an item.
12.4.3 display data in another Dataset
The tdblookuplistbox component and the tdblookupcombobox component display the data in another dataset in the form of a list box and a combo box respectively.
Assume that there is a table named orderstable, which contains a custno field to express the customer's number. However, the orderstable table does not contain the customer's information except the customer's number. Another table is called mermerstable. Besides the custno field, it also contains information such as the customer's company name and address.
The tdblookuplistbox component can implement this function. When you browse records in orderstable, the program first searches for records matching the custno field in customerstable. If the records cannot be found, search for the string that matches the company field from the list. If yes, select this item, as shown in Figure 12.3.
Figure 12.3 tdblookuplistbox component usage
When you select an item in the list, the program searches for the company field that matches the string in customerstable. If you find the item, replace the custno field in the orderstable table with the value of the custno field.
12.5 use check boxes to process Boolean Fields
The tdbcheckbox component can be considered as the data-aware version of The tcheckbox component and is used to process Boolean fields. For example, you can use a check box to indicate whether the customer has paid the account.
The tdbcheckbox component compares the field value with the preset two strings, which are respectively specified by the valuechecked and valueunchecked attributes. If the field value matches the string specified by the valuechecked attribute, select the check box. If the field value matches the string specified by the valueunchecked attribute, the check box is not selected. Note: The valuechecked attribute and the valueunchecked attribute cannot be the same string.
Generally, the valuechecked attribute is set to a string such as "true" and "yes", but it can also be any other string or even a group of strings separated by semicolons, for example:
Dbcheckbox1.valuechecked: = 'true; yes; on ';
In the above case, when the field value matches one of the strings, select the check box. Note that the string specified by the valuechecked attribute is case sensitive.
Generally, the valueunchecked attribute is set to a string such as "false" or "no", but it can also be any other string or even a group of strings separated by semicolons.
If the field value does not match the string specified by the valuechecked attribute or the string specified by the valueunchecked attribute, the check box is grayed out.
12.6 restrict Field Values in the single-choice grouping box
The tdbradiogroup component can be considered as a data-aware version of The tradiogroup component. It can restrict the selection of field values from a group of data.
Like the tradiogroup component, you must first set the items attribute to specify the items to be displayed in the radio group box. The items attribute is a typical tstrings object. Each string corresponds to a button in the single-choice grouping box.
When a user browses a record, if the field value matches the tag of a button in the single-choice grouping box, select this button, as shown in Figure 12.4.
Figure 12.4 select a single sequence that matches the value of the custno Field
In turn, when you select a button in the single-choice grouping box, the program will use the tag of this button to assign a value to the field specified by the datafield attribute.
If you do not want to match the tag of the button with the field value, you can specify other strings, which requires the values attribute. The values attribute is also a tstrings object used to specify a group of strings. When you select a button in the single-choice grouping box, the program assigns a value to the field specified by the datafield attribute with a string in the values attribute, instead of the button tag.
12.7 use the tdbgridtdbgrid component
Displays and edits data in a dataset in a grid. Its appearance depends largely on the following three factors:
. 1. Permanent column object.
Second, it is a permanent field object.
Third, the objectview attribute of the dataset component will affect the display mode of the ADT and array fields.
The most important attribute of the TDBGrid component is columns, which is a tdbgridcolumns object used to manage a group of tcolumn objects. During the design period, you can open an editor to create a permanent column object, and then set the attribute of the column object in the object observer.
12.7.1 Dynamic Column object
If the state attribute of tdbgridcolumns is set to csdefault, the column is dynamically generated. The attribute of the column depends on the attribute of the field. When the attribute of a field changes, the attribute of the column also changes.
The advantage of dynamic generation of columns is that other datasets can be dynamically selected at runtime, without worrying about whether the grid is suitable for displaying new datasets. For example, you can use the same TDBGrid component to display a paradox table and then query the results of another database.
During the design period, you cannot directly modify the attributes of a dynamic column object. You can only modify the attributes of a Field object to indirectly modify the attributes of a dynamic column object.
The lifetime of a dynamic column object also depends on the lifetime of the Field object. If a dataset does not have a permanent field object, all dynamic column objects will disappear when the dataset is closed.
Note: If the state attribute of tdbgridcolumns is set to csdefault at runtime, all column objects are deleted, and column objects are rebuilt based on the field objects in the dataset.
12.7.2 permanent column object
To be able to customize the grid during the design period, a permanent column object is required. After a permanent column object is created, if the state attribute of tdbgridcolumns is set to cscustomized, the attributes of each column can be set independently. For example, by default, the label of the field displayed by the column title is displaylabel. You can re-specify the column title by modifying the caption attribute of tcolumntitle, but the displaylabel attribute of tfield is not affected.
The state attribute of tdbgridcolumns is set to cscustomized, which is suitable for the situations where the structure of those datasets is fixed. If you need to switch between different datasets at runtime, you cannot set the state attribute to cscustomized.
To create a permanent column object, select the TDBGrid component on the form, and click the ellipsis next to the columns attribute in the object observer to open the editor shown in 12.5.
Figure 12.5 create a permanent column object
At the beginning, this editor was blank, because by default, column objects in the grid are dynamically generated and there is no permanent column object.
To create a permanent column object based on each field in the dataset, right-click the column object and choose "add all fields" from the context menu.
To create an independent permanent column object, click the (add new) button on the toolbar. Select the newly created column object, set the fieldname attribute in the object observer to specify a field, and set the caption attribute to specify the column title.
To delete a column object, click the delete selected button on the toolbar. If you delete all permanent columns, the grid will display all fields in the dataset. This is because after permanent columns are deleted, delphi 4 automatically sets the state attribute of tdbgridcolumns to csdefault and dynamically generates all columns. To adjust the Column Display order in the grid, move the column object forward or backward with the mouse.
For a permanent column object, the default value of its attribute is still taken from the field unless you modify the attribute of the permanent column object. For example, by default, the column title is the displaylabel attribute of the field. If you modify the displaylabel attribute of a field, the column title changes accordingly. However, once you modify the column object's caption attribute, the column title is no longer associated with the field's displaylabel attribute, and they are independent of each other.
As mentioned above, when creating a permanent column object, you must set the fieldname attribute to specify a field. However, you can leave the fieldname attribute empty. In this case, the field attribute of the tcolumn object returns NULL, and the column is blank in the grid. Blank columns are often used to display custom content, such as charts.
The fieldname attribute of several column objects can be set to the same field. Therefore, the fieldcount attribute of TDBGrid may be smaller than the number of columns in the grid.
12.7.3 allow users to edit data
For permanent column objects, You can edit data in several ways. The first is to display the data of another dataset in the form of a combo box, the second is to create a static list, and the third is to open a dialog box with the ellipsis button.
To display data of another dataset in a combo box, you must first add a lookup field to the dataset, create a permanent column object, and set its fieldname attribute to specify this lookup field, set its buttonstyle attribute to cbsauto. After the above operations, the grid effect is 12.6.
Figure 12.6 display data of another dataset in the form of a combo box
To create a static list, you do not need to add a lookup field in advance. You only need to create a permanent column object and set the buttonstyle attribute to cbsauto, in the object observer, click the ellipsis next to the picklist attribute to open a string List editor, and then type some strings in sequence.
To use a dialog box for users to select data, you must set the buttonstyle attribute of the column object to cbsellipsis. When the program is running, a ellipsis button appears in this column. Clicking this ellipsis triggers the oneditbuttonclick event. In the handle for processing this event, you can open a dialog box to allow users to select data. For Image Fields, you can click the ellipsis to display an image.
12.7.4 set column object attributes during design
The attribute of the column object determines how the data is displayed in the grid. During the design period, you can set the following attributes:
. Alignment is used to set the Data Alignment. The default value is tfield alignment.
. Buttonstyle is set to cbsauto to add a combox. If it is set to cbsellipsis, an ellipsis is added. When you click this button, oneditbuttonclick is triggered. If it is set to cbsnone, no combo box or ellipsis button exists.
. Color is used to set the background color. The default value is TDBGrid color.
. The number of items in the dropdownrows drop-down list box. The default value is 7.
. Expanded if the column object corresponds to an ADT or array field, this attribute is used to control whether to expand. L fieldname is used to specify a field, which can be left empty.
Setting. readonly to true indicates that the column is read-only and cannot be edited.
. Width is used to set the column width. The default value is displaywidth of tfield.
. Font is used to set the font of Characters in the column. The default value is TDBGrid font.
. Picklist is used to create a static list.
. Title: A tcolumntitle object used to set the column title.
Tcolumntitle has the following attributes:
. Alignment is used to set the title alignment. If it is set to taleftjustify, it indicates the left alignment, tacenter indicates the center, and tarightjustify indicates the right alignment.
. Caption is used to specify the text of the title. The default value is tfield displaylabel.
. Color is used to set the background color of the title. The default value is TDBGrid's fixedcolor.
. Font is used to set the title Font. The default value is TDBGrid titlefont.
12.7.5 display ADT and array Field Values
The TDBGrid component of Delphi 4 fully supports the object fields of Oracle 8.
If the objectview attribute of the dataset component is set to true, the object fields can be expanded or collapsed. When an object field is expanded, the original column is divided into several columns. Each column shows the value of a subfield, and each column contains its own title bar, in this way, we can clearly see the inclusion relationship between fields. When an object field is folded, only one string is displayed. This string lists the values of each sub-field and is separated by commas.
If the objectview attribute of the dataset component is set to false, each sub-field occupies a column like the object field.
The following describes several attributes related to object fields. 1. The expandable attribute of tcolumn. If it is set to true, the column can be expanded. The second is the expanded attribute of tcolumn, which is used to test whether the column is expanded currently. The third is the maxtitlerows attribute of TDBGrid, which is used to set the maximum number of lines of titles in the grid. Fourth, the objectview of tdataset. This attribute has been described earlier. Fifth, the parentcolumn attribute of tcolumn is used to return the column object displaying the parent field.
Figure 12.7 shows how to set the objectview attribute to false:
Figure 12.7 when the objectview attribute is set to false
Figure 12.8 demonstrates the collapse of object fields when the objectview attribute is set to true:
Figure 12.8 when the objectview attribute is set to true but the object field is collapsed
Figure 12.9 shows how the object field is expanded when the objectview attribute is set to true:
Figure 12.9 When the objectview attribute is set to true but the object field is expanded
12.7.6 Set grid options
You can set grid options during the design period, which requires the options attribute of the TDBGrid component. The options attribute is a set that can contain the following elements:
. If dgediting contains this element (the same below), you can modify the data in the grid;
. The dgalwaysshoweditor grid is automatically in the editing status. Otherwise, press f2 to enter the editing status;
. Dgtitles: displays the column title;
. The leftmost end of the current row of dgindicator displays a 4 symbol;
. You can reset the width of the dgcolumnresize column;
. The dgcollines columns and columns are separated by lines;
. Dgrowlines lines are separated by lines;
. Dgtabs can use the tab key and SHIFT + Tab key to move the input focus between columns;
. Dgrowselect users can select a whole row; otherwise, only one unit can be selected;
. Dgalwaysshowselection the selected unit remains in the selection status even if the input focus is removed;
. Dgconfirmdelete: press Ctrl + Delete to delete a row. A confirmation box is displayed for the user to confirm;
. When dgcancelonexit exits the grid, any pending changes will be canceled.
. Dgmultiselect users can select multiple rows in the grid, which is equivalent to a checklist box.
12.7.7 respond to user actions at runtime
The TDBGrid component has several events for responding to user actions. Because the grid consists of rows and columns, In the event handle, you must first distinguish the column where the current is. These events are listed below:
. Oncellclick this event is triggered when you click a cell.
. Oncolenter this event is triggered when the input focus enters a column.
. Oncolexit triggers this event when the input focus leaves a column.
. Oncolumnmoved this event is triggered when you move a column to another position.
. Ondblclick this event is triggered when you double-click in the grid.
. Ondragdrop this event is triggered when the user releases the dragged object in the grid.
. Ondragover this event is triggered when you drag an object through the grid.
. Ondrawcolumncell this event is triggered when a unit needs to be repainted.
. Ondrawdatacell triggers this event when the state attribute is set to csdefault and a unit needs to be re-painted.
. Oneditbuttonclick this event is triggered when you click the ellipsis button.
. Onenddrag triggers this event when the user releases the mouse.
. Onenter this event is triggered when the grid gets the input focus.
. Onexit this event is triggered when the grid loses the input focus.
. Onkeydown this event is triggered when you press the next key.
. Onkeypress this event is triggered when you press the next visible character key.
. Onkeyup this event is triggered when the user releases a key.
. Onstartdrag this event is triggered when the user starts the drag and drop operation.
. Ontitleclick this event is triggered when you click the title of a column.
12.7.8 a Special Database Grid
The TDBGrid component displays only one record in one row, but you can set the attributes of each column. The feature of the tdbctrlgrid component is that each row is a single pane (tpanel object) and some controls can be placed on the pane. For example, you can place an tdbedit component on the pane to display the value of a field. You can also add a tlabel component next to the edit box as a tag. Another feature of tdbctrlgrid is that one row can have multiple panes, that is, one row can display multiple records, and the height and width of the pane can be adjusted.
First, put a tdbctrlgrid component on the form and set its datasource attribute to specify a tdatasource component. At the beginning, the tdbctrlgrid component had three rows and one column.
Next, you need to place some data controls on the "design unit" of the tdbctrlgrid component and set the datafield attribute of these data controls to specify the fields to be displayed.
A "design unit" is usually the top or leftmost unit. The difference with other units is that it does not have diagonal lines.
Note: Only the data control can be placed on the "design unit" of the tdbctrlgrid component. After the program is compiled and run, the Data Control on the "design unit" is automatically copied to other units, different records are displayed for each unit.
Figure 12.10 demonstrates the usage of the tdbctrlgrid component:
Figure 12.10 tdbctrlgrid component usage
The attributes of the tdbctrlgrid component are listed below:
. If allowdelete is set to true, records can be deleted.
. If allowinsert is set to true, records can be inserted.
. Colcount is used to set the number of panes for each row. The default value is 1.
. Orientation: Set govertical to show records from top to bottom. Set gohorizontal to show records from left to right.
. Panelheight is used to set the height of the pane. The default value is 72.
. Panelwidth is used to set the width of the pane. The default value is 200.
. Rowcount is used to set the number of rows. The default value is 3.
If showfocus is set to true, a dotted border is displayed on the current record pane.
. Selectedcolor is used to set the background color of the pane that displays the current record.
12.8 pilot
The navigator is implemented using the tdbnavigator component, which allows users to conveniently browse and manipulate records. The navigator can contain up to 10 buttons, including:
. First, the first record;
. Prior previous record;
. Next next record;
. Last last record;
Insert a blank record;
. Delete: Delete the current record;
. Edit allows you to edit the current record;
. Post makes the pending edit operations valid;
. Cancel cancels pending edits;
. Refresh refresh the record with the latest data in the dataset.
You can select some buttons on the navigator as needed, which requires the visiblebuttons attribute.
During the design period, you can select the button to be displayed on the navigator in the object observer. During runtime, you can select the button to be displayed on the navigator through programming. For example, assume that you want to use the same navigator to navigate two datasets, one of which allows users to edit data, and the other does not allow users to edit data. When switching between two datasets, the buttons on the navigator should be dynamically changed, because for datasets that do not allow users to edit data, buttons such as insert, delete, edit, post, cancel, and refresh should not appear on the navigator. The following code hides these buttons:
Procedure tform1.customercompanyenter (Sender: tobject );
Begin
If sender = customercompany then
Begin
Dbnavigatorall. datasource: = customercompany. datasource;
Dbnavigatorall. visiblebuttons: = [nbfirst, nbprior, nbnext, nblast];
End
Else
Begin
Dbnavigatorall. datasource: = ordernum. datasource;
Dbnavigatorall. visiblebuttons: = dbnavigatorall. visiblebuttons + [nbinsert, nbdelete, nbedit, nbpost, nbcancel, nbrefresh];
End;
End;
By the way, when the same navigator needs to navigate several datasets, it is necessary to dynamically set the datasource attribute. Assume that the form has two tdatasource components: customerssource and orderssource. Their dataset attributes specify the customerstable table and orderstable table respectively. In addition, the form has two tdbedit components, they are called mermersedit and ordersedit respectively. Their datasource attributes point to customerssource and orderssource respectively. The following program writes a common event handle for the two tdbedit components to handle the onenter event. When you edit the event on customersedit, the navigator will be the customerstable navigation, when you edit it on ordersedit, the navigator is the orderstable navigation.
Procedure tform1. customereditenter (Sender: tobject );
Begin
If sender = customeredit then
Dbnavigatorl. datasource: = customeredit. datasource;
Else
Dbnavigator1.datasource: = orderedit. datasource;
End;
12.9 data sources
The tdatasource component is a non-visual component that serves as a bridge between a dataset and a data control. Each data control must specify a data source (tdatasource component). Correspondingly, the dataset attribute of the tdatasource component must specify a dataset. The following describes the attributes and events of the tdatasource component.
The dataset attribute is used to specify a dataset. During the design period, you can select a dataset for the dataset attribute in the object observer. During the runtime, You can dynamically select a dataset through the code. The program example is as follows:
With custsource do
Begin
If dataset = 'customer' then
Dataset: = 'Orders'
Else
Dataset: = 'customer ';
End;
You can also specify the dataset component on another form, for example:
Procedure tform2. formcreate (Sender: tobject );
Begin
Performance1.dataset: = form1.table1;
End;
Generally, the tdatasource component name is irrelevant. However, the name of the tdatasource component should reflect the connected dataset. For example, if the dataset connected by the tdatasource component is MERs, the name of the tdatasource component is preferably customerssource.
The enabled attribute is used to control whether the tdatasource component is connected to the dataset. If it is set to true, the connection is established. If it is set to false, the connection is closed temporarily. If the enabled attribute is set to false, all data controls connected to the data source become blank.
If the autoedit attribute is set to true, when you type a character in the data control, the dataset automatically enters the dsedit state. If the autoedit attribute is set to false, the program must call the Edit function to enter the dsedit state.
The ondatachange event is triggered when the position of the current record of the dataset changes, probably because the program calls methods such as next, previous, and insert.
The onupdatedata event is triggered when the data of the current record is to be updated. This may be because post is called. Data can be verified in the handle that handles this event.
The onstatechange event is triggered when the state attribute changes. For example, you can use a label to dynamically display the current status. The program example is as follows:
Proceduretform1.performance1. statechange (Sender: tobject );
VaR
S: string;
Begin
Case custtable. State
Dsinactive: S: = 'inactive ';
Dsbrowse: S: = 'browse ';
Dsedit: S: = 'edit ';
Dsinsert: S: = 'insert ';
Dssetkey: S: = 'setkey ';
End;
Custtablestatelabel. Caption: = s;
End;
The onstatechange event can also allow or disable buttons and menu items dynamically. The program example is as follows:
Procedure form1.performance1. statechange (Sender: tobject );
Begin
Custtableeditbtn. Enabled: = (custtable. State = dsbrowse );
Custtablecancelbtn. Enabled: = custtable. State in [dsinsert, dsedit, dssetkey];
...
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.