OnChange

We have seen OnChange before but it is also an important facet of managing the Rich Edit component. When the event is fired, the text property of the control may have changed and the event handler is in a position to manage the change. The application should verify that actual change occurred by querying the Modified property

Putting DBText, DBEdit, and DBRichEdit to Work

Being able to simply view items in a database will not satisfy many clients. They keep wondering if there isn't some way that the application could allow them to modify the records or even add new data. Dig out the Venue Manager application-this next exercise is going to add these capabilities to the program. Whenever designing major changes into an application, the business rules that control the transactions must be reviewed to ensure that all will be met by the architecture.

When a new event is scheduled, it must be for a venue that exists on the list. The referential integrity of the application depends on this requirement, as this is the common field creating the relationship between the two tables. To maintain the integrity of the EVENTS table, the event number must also be unique; the tables selected for use in this program will handle that requirement on their own. The data .. , , of the EventNd field is set to Au ^Increment J making it automatically read-only and guaranteed unique. Finally, any event added to the database cannot conflict with another scheduled event for the selected venue.

What rules apply to the modification of events that already exist in | database? First, with the exception of fully deleting the record, the event number should not be subject to modification. \|| w int| the | ber to be modified would subject the application I the risk of ntegritj problems. The venue number should also remain mlv 1 avoid referential integrity problems but it does reveal an p r nitv| for a new feature, the possibility of triggering an addition to the Venues list wbenl an unknown number is added. The last concern . I editing existing records mirrors that of the addition process; changes I a record cannot allow it to conflict with an existing event. Managing these constraints is easy when the triggering operations are on the surface, such as when we add new forms and controls, and not sa e a ;ifteii the i has been integrated into the program.

1. Reopen the VenMgr application and add a new form, ¡'his form will he named EditEventF and the associated unit should be saved ¿is F.ditEventU.pas. To give you an idea of what we .ire working towards, the completed form is shown in Figure 6.8

/ Event Scheduling

Memorial Stadium

Figure 6.8

The completed Event Scheduling form of the Venue Manager project

Event Name

Brochure Description

Tc-.lJ I 'ft-: ' Ht-, >:n-te Í^gh scoring game :.r 1 soinh

Event Date

Event Time Ticket Price omen's Basketball Finals

Publicity Photo

Publicity Photo

Chapter 6—De/phi's Datfl-Awarel Components I I 87

mmmmmm

2. Associate rhe data module with the new form so that it has access to the shared data tables. Under the Implementation section of the EditEventU unit, enter the following line:

Uses dmodu:

Switch your attention to the main form for the application, Forml. Add the EditEventU unit to the Uses statement for this application, either by typing the code in by hand or selecting File | Use Unit from the menu.

a Button control to the form somewhere in the vicinity of the Events grid. Caption the button Edit Events and then double-click on it to access the OnClickl event handler. The application uses this button to pop up the event editing form, so add the following code to the procedure:

procedure TForml.Button LCIick(Senderi T0bject)J begin

I TEditEventF .Create (Application) ;

EditEventF.Show; end;

Test the button to ensure that your form will pop up when needed. If everything is working as planned, switch your attention back to the edit form and add the following line to the form's QnClosd event handler:

procedure TEd itEventF .FormC lose (Sender: TObject; var Act ion : TCIoseAction)J begin

EditEventF.Release;

End;

The first components we will add are shown in Figj ure 6.9.

A DBTextl component is going to be used to display the currently selected venue because there is no need to edit the field, nor do we want the user to have that kind of access. Position the component in the upper- left corner as shown and set the DataSourcd to DataModtllej 1 .VENfUE_SRC and the DataField to Venue. Set the AutoSizel property to True so that it will accommodate the variously sized labels that will be encountered and the Font Size property to 12 to make the label more prominent.

I : Memorial Stadium]

  • j Event Number |, j ]
  • i «
  • Hi 1 ■ - .

Figure 6.9 Adding a componer^l

Add a Label control just below the venue name and set the Caption property to Event Number. Increase its font size to 10. Immediately! below this label the Event Number field is going to be added. To meej the business rules for this field, we want the field to remain under the control of the program and not the user. Again we can use a DBTextl toi display the field in a read-only format that will prevent tampering. Add the component and set the Data Soured property to DataModuIel.EVENT_SRC and DataFieldl to EventNo. For the sake of appearance, change the Alignment property to taCenterJ

Add four more labels distributed as shown in Figure 6.8 for the remaining edit fields in the table. Caption them as Event Name, Event Date, Event Time, and Ticket Price. Set the font size to i ^ id match the other label.

Add four DBEdid components to the form, distributing them below the labels that were just added. Set the DataSourcd on all of them to DataModulel.EVENTSRC The properties of the controls from left to right are:

I DBEditll

m DBEdit2

  • Data Field = Event-Date m DBEdit3
  • DataFieldl = Event-Time m DBEdit4
  • DataField = Ticket-price

Add another label, setting the font attributes to match the others and the caption to Brochure Description. Below this label add a DBRichEdit control. Set the DataSource property to DataMod-l ule 1 .EVENT SRC] and the DataField property to Event-Description. Size the component as appropriate for the data. The application's values are set to a Height of 135 and a Width of 240.

To the right of this pair of controls, place another label with a font size of 10 and set the caption to read Publicity Photo. Below this we want to display a picture so add a DBImag^ control, setting the DataSource to DataModule 1 .EVENTSRC and the DataFieldl property to Event-Photo. A good size for the images contained in the database is to set the Height at 135 and the Width at 200.

The last component to add to this form for now is a DBNavigato| Set the Align property to alBottoml so that the control will hug the bottom

Chapter 6—Delphi's Data-Aware Components of the form. Compile and execute the application to ensure that everything is working as planned. Notice that because we have the relationship between the Venues and Events set up in the data module, when you switch to the edit form you are only able to access those events for the currently selected venue.

The event handlers and methods of the components are used to implement the integrity rules in this application. It would be common for the developer to set many business rule items at the table level, close to the data. The integrity constraints such as date or time conflicts discussed earlier would be best handled through code additions to the event handlers of the specific field. The date, for example, could be tested against the other elements in the same column to determine if a match occurs. If so, an error message would be displayed explaining the conflict and the focus returned to the date field. A good place to put this code would be in the OnExit handler. After a date is entered and the user tabs over to the next field, the event would be triggered.

To explore some of the capabilities of the DBRichEdit control, we'll set up a way in which the contents of the field can be modified. This means adding a couple of additional components to the Event Scheduling form.

Add a FontDialog and a PopupMenu component to the form. Both of the controls are non-visual so their placement is not important. Select the DBRichEdit control and set the PopupMenu property to PopupMenu 1.

Double-click on the popup control to start the Menu Designer and add a single entry that is captioned Font. Double-click on the Font entry so that its OnClick handler can be modified. Add the following code to the procedure:

procedure TEditEventF .FontlC lick (Sender: TObject); begin if FontDi alogl.Execute then begin

  1. SelAttributes.Col or :=
  2. Font.Color; DBR ichEditl.SelAt tributes .Size :=

FontD ialogl.Font.Size; DBR ichEditl.SelAt tributes .Style := FontDi alogl.Font.Style;

190 ■ Part II-The Delphi Database Tools

The SelAttributes property refers to the text that is selected by the user If you want to affect all of the text in the control you should address the Font properties of the DBRichEditl control directly.

3. Compile and execute the modified program. Select some of the memo text by using the keyboard or the mouse and then right-click on the DBRichEdit control. This will pop up the menu, which can certainly be a good deal more detailed, from which you can start the Font dialog. Set the properties the way that you want them to appear and click OK. If you press the Cancel button, the code will bypass implementing the property changes.

Was this article helpful?

0 0

Post a comment