home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
DOS/V Power Report 1996 August
/
VPR9608A.BIN
/
del20try
/
install
/
data.z
/
DATAEDIT.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1996-05-08
|
4KB
|
149 lines
unit DataEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, Calendar, DB, DBTables;
type
TDBRWCalendar = class(TCalendar)
private
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
procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
X, Y: Integer); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure Change; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TDBRWCalendar]);
end;
constructor TDBRWCalendar.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 TDBRWCalendar.Destroy;
begin
FDataLink.Free; { dispose of data-link object }
inherited Destroy; { then call inherited destructor }
end;
procedure TDBRWCalendar.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 TDBRWCalendar.GetDataField: string;
begin
Result := FDataLink.FieldName; { pass through field name from data link }
end;
function TDBRWCalendar.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource; { pass through data source from data link }
end;
procedure TDBRWCalendar.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value; { pass through field name to data link }
end;
procedure TDBRWCalendar.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 TDBRWCalendar.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 TDBRWCalendar.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 TDBRWCalendar.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 TDBRWCalendar.Change;
begin
FDataLink.Modified; { tell data link that data changed }
inherited Change; { and call inherited, which calls event handler }
end;
procedure TDBRWCalendar.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.