home *** CD-ROM | disk | FTP | other *** search
- unit DataEdit;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms,
- Dialogs, Grids, Calendar, DB, DBTables;
-
- type
- TDBCalendar = class(TCalendar)
- private
- { Private declarations }
- FDataLink: TFieldDataLink; { field for data link }
- procedure DataChange(Sender: TObject); { called when data changes }
- function GetDataField: string;
- function GetDataSource: TDataSource;
- procedure SetDataField(const Value: string);
- procedure SetDataSource(Value: TDataSource);
- procedure UpdateData(Sender: TObject); { called when control changes }
- procedure CMExit(var Message: TCMExit); message CM_EXIT; { called to update data }
- protected
- { Protected declarations }
- procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
- X, Y: Integer); override;
- procedure KeyDown(var Key: Word; Shift: TShiftState); override;
- procedure Change; override;
- public
- { Public declarations }
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- published
- { Published declarations }
- property DataField: string read GetDataField write SetDataField;
- property DataSource: TDataSource read GetDataSource write SetDataSource;
- end;
-
- procedure Register;
-
- implementation
-
- procedure Register;
- begin
- RegisterComponents('Samples', [TDBCalendar]);
- end;
-
- constructor TDBCalendar.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner); { Always call the inherited constructor }
- FDataLink := TFieldDataLink.Create; { construct data-link object }
- FDataLink.OnDataChange := DataChange; { attach method to event }
- FDataLink.OnUpdateData := UpdateData; { attach method to event }
- end;
-
- destructor TDBCalendar.Destroy;
- begin
- FDataLink.Free; { dispose of data-link object }
- inherited Destroy; { then call inherited destructor }
- end;
-
- procedure TDBCalendar.DataChange(Sender: TObject);
- begin
- if FDataLink.Field = nil then { if there is no field assigned... }
- CalendarDate := 0 { ...set to invalid date }
- else CalendarDate := FDataLink.Field.AsDateTime; { otherwise, set to new data }
- end;
-
- function TDBCalendar.GetDataField: string;
- begin
- Result := FDataLink.FieldName; { pass through field name from data link }
- end;
-
- function TDBCalendar.GetDataSource: TDataSource;
- begin
- Result := FDataLink.DataSource; { pass through data source from data link }
- end;
-
- procedure TDBCalendar.SetDataField(const Value: string);
- begin
- FDataLink.FieldName := Value; { pass through field name to data link }
- end;
-
- procedure TDBCalendar.SetDataSource(Value: TDataSource);
- begin
- FDataLink.DataSource := Value; { pass through data source to data link }
- end;
-
- { UpdateData
-
- UpdateData is only called after calls to both FDataLink.Modified and
- FDataLink.UpdateRecord. }
-
- procedure TDBCalendar.UpdateData(Sender: TObject);
- begin
- FDataLink.Field.AsDateTime := CalendarDate; { set field data to calendar date }
- end;
-
- { MouseDown
-
- Only process the mouse-down if the data link can edit the data. Otherwise,
- just call the event handler to let the user handle the mouse-down event. }
-
- procedure TDBCalendar.MouseDown(Button: TMouseButton; Shift: TShiftState;
- X, Y: Integer);
- var
- MyMouseDown: TMouseEvent;
- begin
- if not ReadOnly and FDataLink.Edit then
- inherited MouseDown(Button, Shift, X, Y)
- else
- begin
- MyMouseDown := OnMouseDown;
- if Assigned(MyMouseDown) then MyMouseDown(Self, Button, Shift, X, Y);
- end;
- end;
-
- { KeyDown
-
- Only process the key-down if the data link can edit the data. Otherwise,
- just call the event handler to let the user handle the key-down event. }
-
- procedure TDBCalendar.KeyDown(var Key: Word; Shift: TShiftState);
- var
- MyKeyDown: TKeyEvent;
- begin
- if not ReadOnly and (Key in [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_END,
- VK_HOME, VK_PRIOR, VK_NEXT]) and FDataLink.Edit then
- inherited KeyDown(Key, Shift)
- else
- begin
- MyKeyDown := OnKeyDown;
- if Assigned(MyKeyDown) then MyKeyDown(Self, Key, Shift);
- end;
- end;
-
- procedure TDBCalendar.Change;
- begin
- FDataLink.Modified; { tell data link that data changed }
- inherited Change; { and call inherited, which calls event handler }
- end;
-
- procedure TDBCalendar.CMExit(var Message: TCMExit);
- begin
- try
- FDataLink.UpdateRecord; { tell data link to update database }
- except
- SetFocus; { if it failed, don't let focus leave }
- raise;
- end;
- inherited;
- end;
-
- end.
-