The DBRadio Group Component

The DBRadioGroup component is a data-aware version of the RadioGroup control. Similar to the data-aware list components, it presents the user with a limited choice of selections for a data field. The user selects the value that they want to enter for the field by select ing one of a group of radio buttons. A radio button interface should only be used in situations where the number of choices is very limited. The mutually exclusive nature and default selection options of these controls require familiarization and do not work well for more than a handful of choices. When the collection of possible values exceeds the number of fingers on your hand, use a list box.

200 ■ Port l/-The Delphi Database Tools

The display actions of the DBRadioGroup control differ significantly from the list components. If the value of the underlying database field matches one of the elements in the control's Items property, the corresponding button will be selected when the data is displayed. In addition to the Items string list, there is a second property called Values. The string collection entered in the Values property are the values that will be passed to the data field in lieu of the strings in the Items property. The value that is sent is selected on the basis of the index value of the button that is pushed.

This second string list can cause some confusion at first glance. If the value in the data field does not match an element from the Items list or the Values list if it exists, no button will be selected. However, if the Values list exists and the data field value does not match an element in the Items list but does match an element in the Values list, a button wfl be selected.

Key Properties

Columns

The radio controls in Delphi do not need to be designed vertically only; they have the capability of spreading over multiple columns horizontally as well. The Columns property specifies the number of columns for the DBRadioGroup control. The maximum number of columns is 16,

Items

The Items property contains the string list that describes each of the buttons in the radio group. For each item in the list, a button will be created. In the absence of a list in the Values property, the elements of the Items string list are the data values that will be written to a field specified in the DataField property

Value

The Value property contains the content of the current data field. If a different radio button is selected, the value of this property will change. This property can be queried to retain a value before a change is made.

Values

In some cases your application will require that the value that is written to the data field is different from the caption of the button. This might be for simplification or clarity purposes. When this is the case, the string list that defines the actual data values to be written is

Chapter 6—Dc/phi's Data-Aware Components Hi 20

Chapter 6—Dc/phi's Data-Aware Components Hi 20

defined in the Values property. The elements contained in this property must correspond one-to-one with the elements in the Items property in order to function correctly. The value that is selected from the list will match the Itemlndexl value.

To cement your understanding of the data-aware radio group control, you might want to assemble the following quick demonstration project. It uses the RHPS.DB table, located in the Chapter6 directory of the CD-ROM.

Start a new application and add a Data Source J Table, Navigator, DBGridJ and two DBRadioGroup) components on the form as shown in Figure 6.13.

The Table setting for the DatabaseNamd will be DBDEMOS and the TableNamel is RHPS.DB J Point the DataSource to Table 1 and point all of the remaining controls to DataSource 11 Activate the table.

Select the first radio button control and set the Caption property to WHO. This component is going to control what values get entered into the WHO field of the database. Set the Data Field property to WHO and then double-click on the Items property, starting the String Editor. Add the following strings to the list, each on its own line: Riff Raff, Columbia, and Magenta.

Select the second DBRadioGronp| component on the form and set the caption on it to read WHAT. Set the DataFieldl property to WHAT and duplicate the string list from the previous control in the Items property. In this group the value written to the underlying datasatl field is going to come from the Values property rather than the Items list. Double-click on the Values property to get to the string editor and add the following elements to the list: Handyman, Entertainer, and Housekeeper.

202 ■ Part H—The Delphi Database Tools

4. Compile and execute the application. The first selection is very straightforward; the user clicks on a button and the value shown as the caption of the radio button is transferred to the database. T ond radio group is a little more furtive. Rather than sending the value shown to the database, the element corresponding to the button's Itemlndex in the Values property is written to the data field.

The DBCtrlGrid Component

Saving the DBCtrlGrid for last makes the most sense, as the component itself is a container for a select group of the other data-aware controls. DBCtrlGrid begins with the fundamental concept of the grid, that is, to display a number of columns of numerous rows simultaneously. This data is configured in a very familiar table layout that conveys the information to the user simply and clearly once they have divined the relationship between the rows and columns. The control grid begins with this premise and then kicks it up several notches.

The DBCtrlGrid control is composed of panels on which a selection of the other data-aware (or non-data-aware) controls can be placed. At design time, the controls on the panel represent a single row in the dataset. At run time, however, the layout that you create is duplicated for every row in the underlying dataset, filling the component with a view of multiple fields from multiple records. The navigation controls that are used will cause the display to skip from panel to panel.

The DBCtrlGrid component provides a unique interface opportunity for your applications. If single field controls are placed directly upon a form, the user can only view a single record in that format at a time. There are many times when this is appropriate. In some cases, however, the information is better presented as discrete fields but in a way that more than one row of the database can be displayed at one time. For these times, Borland has included the DBCtrlGrid.

Key Properties

The DBCtrlGrid component introduces some new concepts and properties to the data-aware VCL controls. Be aware that the control grid is a container object like a panel and that some of the properties set for the container will override the properties of the individual controls.

Chapter 6--Delphi's Data-Aware Components ■ 2

AllowDelete

The setting in the AllowDelete property determines whether or not the current record can be deleted from the dataset by pressing Ctrl-Delete. When False, no deletion can occur through the DBCtrlGrid, either through the aforementioned key combination or through the DoKey method passed with the applicable parameters. If the property is True, the default value, the row can be deleted by the key combination or use of the DoKey method.

The DoKey method matches a keystroke combination to a method to perform specific actions. The action is specified by the Key parameters and supports the activities shown in Figure 6.14. A number of these actions are specific to navigating within the DBCtrlGrid component, which can become complicated when the interface has been separated into columns and rows.

gkNull

GkEditMode

Do nothing

Toggle the EditMode property gkNull

GkEditMode

Do nothing

Toggle the EditMode property gkPriorTab

Move to the previous panel gkPriorTab

Move to the previous panel

gkNextTab

Move to the next panel

gkLeft

Move one panel to the left

gkRight

Move one panel to the right

gkUp

Move one panel up

gkDown

Move one panel down

gkScrollUp

Move one panel up

gkScrollDown

Move one panel down

gkPageUp

Move ColCount * RowCount number of records up ill the dataset. (Page measured by Columns * Rows)

gkPageDown

Move ColCount * RowCount number of records down in the dataset. (Page measured by Columns * Rows)

gkHome

Move to the first row in the dataset

gkEnd

Move to the last row in the dataset

gklnsert

Insert a new row in the dataset above the current row. Set EditMode to True.

gkAppend

Adfcti a new/ roywaltttte end of the dataset. Set EditMode to True.

Bétesetttee aurreettjiyselected row from the dataset. Set the EditMode to False.

gkAppend

gkDelete gkCancel gkDelete gkCancel

Adfcti a new/ roywaltttte end of the dataset. Set EditMode to True.

Bétesetttee aurreettjiyselected row from the dataset. Set the EditMode to False.

Set the EditMode to False and cancel any modifications that have not been written to the dataset.

Figure 6.14 The DoKey actions

Allowlnsert

The twin property of the previous property, Allowlnsert controls the ability to insert new rows by using the Ctrl-Insert key combination or the GoKey method. When the property is set to False, no insertions are allowed through the grid. Setting the property to True and placing the, dataset into Edit mode will enable the user to insert new rows.

ColCount

The DBCtrlGrid is capable of subdividing the panels into columns, creating smaller panels. Each of the smaller panels continues to represent a single row in the dataset. The new panels will fit themselves within the original panel, constrained by the Width property of the component.

Orientation

The Orientation property determines the order in which the rows of the dataset are displayed by DBCtrlGrid. If the property is set to goVertical, the panels will be ordered in rows and the grid will display a vertical scroll bar. When the property is set to goHorizontal, the panels are arranged in columns with a horizontal scroll bar displayed.

Pane/Height

The PanelHeight property determines the height of the panels. This number is different from the Height property for the entire DBCtrlGrid control.

Pane/Width

Similar to the PanelHeight property, the PanelWidth property carries the value that determines the width, in pixels, of the panel. Again, it a different value from the grid's Width property

Chapter 6-Delphi's Data-Aware Components

SelectedColor

Changing the color of the panel can highlight the panel that contains the currently selected row. The SelectedColor property contains the current color value and should be modified to change the default shade.

ShowFocus

The ShowFocus property determines whether or not a focus rectangle is drawn in the current panel to indicate the currently selected record. Set the value to True to display the focus rectangle. When the value is False, the application should indicate the selected panel through some other visual cue.

OBCtrlGrid Events and Related Methods

Just as a number of the properties are unique to this control, there are events not seen previously that only make sense in the context of the DBCtrlGrid.

OnPaintPanel

The OnPaintPanel event fires whenever a panel needs to be redrawn in the grid. The handler allows the developer to customize the paint process. This handler does not affect the controls contained by the panel, as they are drawn on their own.

GetTabOrderList

Executing GetTabOrderList overrides the default to remove all of the controls contained by the grid from the tab order. This allows the application to gain more control over the navigation of the database.

KeyDown

This method performs special processing within the application when specific keys are pressed. The keys handled and their associated actions are displayed in Figure 6.15. The KeyDown method translates the key presses into a logical key code and calls the DoKey method to perform the associated action.

Key and Shift Values

LeftArrow

Key Code

RightArrow

Part II-The Delphi Database Tools

Key and Shift Values

UpArrow

Key Code

UpArrow

DownArrow

gkDown

PageUp

gkPageUp

PageDown

gkPageDown

Home

gkHome

End

gkEnd

Return

gkEditMode

F2

gkEditMode

Insert

gkAppend

Ctrl + Insert

gklnsert

Ctrl + Delete

gkDelete

Escape

gkCancel

All other keys

gkNull

figure 6. IB Key and Shift values handled by the KeyDown method figure 6. IB Key and Shift values handled by the KeyDown method

Putting DBCtrlGrid to Work

The DBCtrlGrid is an esoteric component rife with possibilities, so any minor demonstration is going to seem insignificant. While this is certainly true, the ticket sales demo will, at the very least. give you an idea of how the component works so that you might consider it as a solution to your next design. The application that we are going to build is the last piece we will construct for the Venue Manager application. The program written here is a stand-alone application meant to run on a ticket kiosk with a touch screen where the purchasers can scroll through the events scheduled and then press the bar that represents the event to order tickets.

Start a new application and add a DBNavigator and a DBCtrlGrid to the form. Set the Align property for the navigator to alBottom and the same property of the control grid to alClient. The form should be nicely filled with both of these components now. Save your project.

Add two Table components and a DataSource component to the form. Tablel should have the DatabaseName property set to DBDEMOS and the TableName property set to EVENTS.DB. Table2 will have the same database name and the TableName property set to VENUES.DB. Point the DataSource component to Tablel.

Key Code

Figure 6. 16 The Tickets moiri form

Figure 6. 16 The Tickets moiri form

Chapter 6—Delphi's Data-Aware Components || 2071

Set the DataSource property of the navigator to DataSourcelJ The DBCtrlGridl is a panel-based container control. When the DataSourcel property is set to DataSourcelJ this becomes the DataSource property for all of the child components and cannot be modified.

We need a little information from the VENUES table so a lookup field will be created. Select Table 1 and invoke the Fields Editor. Add all of the existing fields and then click on New Field from the context menu. Name the new field Venue, set the Type property to String, and make the length 25. Be sure to check the Lookup radio button to access the rest of the properties. The DataSer property will be Table2j the Key and Lookup fields will be VenueNo, and the Result field will be Venue. This will return the name of the venue so that it can be displayed on each panel.

Have a look at Figure 6.16 to see how the remaining components are going to be laid out.

The top panel of the grid is the one on which all of your layout should occur so that the desired behavior of the control grid is retained. The striped panels represent the placement of the other rows of the database and are shown for your design reference.

As shown, add the appropriate components to the top panel of the grid. There are some limitations to the DBCtrlGrid; foremost, the following components cannot be used on the grid:

Part /i-The Delphi Database Tools

  • DBGrid
  • DBNavigator
  • DBListBox
  • DBRadioGroup
  • DBLookupList
  • DBRichEdit

All of the fields, with the exception of the DBImage, are DBText controls so that they cannot be modified.

  1. Set the SelectedColor property to clWhite so that the current record will really stand out on the screen.
  2. Finally, this application will use the OnKeyPress event handler to emulate the touch screen. We will trap for the "B" (bar) key in the handler and simply pop up a message. Add the following code to the event handler procedure:

procedure TForml .DBCtrlGr id lKeyPress (Sender: TObject; var Key:

MessageDlg(1 Ticket Master down for maintenance.', mtError,[mbOk],0);

end;

9. Compile and execute the application. Your running application should closely match that shown in Figure 6.17.

Summary

Use all of the different navigation options including the Up arrow, the Down arrow, and the PgUp and PgDown| keys so that you can become familiar with their actions.

Summary

This chapter has covered all of the data-aware controls and, hopefully, provided the usage information needed to put them to effective use in your applications. The projects that were built in the chapter have demonstrated the interplay between the data access controls and the interface components. The key to successfully designing effective Delphi database applications is to understand the interplay between these separate but interlocked groups of components. The interface components, those data-aware controls we have just reviewed, provide the windows through which the user sees their data. The management of that data is centered in the data access controls we explored in the previous chapter. Recognizing this difference in task assignments gives the developer direction when deciding where a specific job is to be performed in a program.

Was this article helpful?

0 0

Post a comment