S
swean_98
Unregistered / Unconfirmed
GUEST, unregistred user!
[8D][8D] 我编写这个控件的目的是使这DBDateTimePiker可以有读写数据库的功能,可是现在的这个控件只能读取,
不能将内容写入数据库!先将代码公布如下,请各位高手指教!
unit DBDateTimePicker;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,DBCtrls,DB;
type
TDBDateTimePicker=Class(TDateTimePicker)
private
FFocused: Boolean;
FAlignment: TAlignment;
FDataLink: TFieldDataLink;
FPaintControl: TPaintControl;
procedure DataChange(Sender: TObject);
procedure EditingChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure UpdateData(Sender: TObject);
protected
procedure Change; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Field: TField read GetField;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
end;
procedure Register;
implementation
constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
end;
destructor TDBDateTimePicker.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
function TDBDateTimePicker.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TDBDateTimePicker.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
function TDBDateTimePicker.GetField: TField;
begin
Result := FDataLink.Field;
end;
procedure TDBDateTimePicker.DataChange(Sender: TObject);
begin
if FDataLink.Field <> nil then
begin
if FAlignment <> FDataLink.Field.Alignment then
begin
FAlignment := FDataLink.Field.Alignment;
end;
if not (csDesigning in ComponentState) then
begin
if (FDataLink.Field.DataType in [ftString, ftWideString]) then
end;
if FFocused and FDataLink.CanModify then
Date := StrToDate(Trim(FDataLink.Field.Text))
else
begin
Date := StrToDate(FDataLink.Field.DisplayText);
end;
end else
begin
FAlignment := taLeftJustify;
if csDesigning in ComponentState then
Date := StrToDate(Trim('2000-1-1'))
end;
end;
procedure TDBDateTimePicker.EditingChange(Sender: TObject);
begin
end;
procedure TDBDateTimePicker.UpdateData(Sender: TObject);
begin
FDataLink.Modified;
FDataLink.Field.Text :=DateToStr(Date);
end;
procedure TDBDateTimePicker.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
if (Key = VK_DELETE) or ((Key = VK_INSERT) and (ssShift in Shift)) then
FDataLink.Edit;
end;
procedure TDBDateTimePicker.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if (Key in [#32..#255]) and (FDataLink.Field <> nil) and
not FDataLink.Field.IsValidChar(Key) then
begin
MessageBeep(0);
Key := #0;
end;
case Key of
^H, ^V, ^X, #32..#255:
FDataLink.Edit;
#27:
begin
FDataLink.Reset;
Key := #0;
end;
end;
end;
procedure TDBDateTimePicker.Change;
begin
inherited Change;
FDataLink.Edit;
end;
procedure Register;
begin
RegisterComponents('Swean',[TDBDateTimePicker]);
end;
end.
不能将内容写入数据库!先将代码公布如下,请各位高手指教!
unit DBDateTimePicker;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,DBCtrls,DB;
type
TDBDateTimePicker=Class(TDateTimePicker)
private
FFocused: Boolean;
FAlignment: TAlignment;
FDataLink: TFieldDataLink;
FPaintControl: TPaintControl;
procedure DataChange(Sender: TObject);
procedure EditingChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure UpdateData(Sender: TObject);
protected
procedure Change; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Field: TField read GetField;
published
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
end;
procedure Register;
implementation
constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
end;
destructor TDBDateTimePicker.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;
procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;
function TDBDateTimePicker.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TDBDateTimePicker.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
function TDBDateTimePicker.GetField: TField;
begin
Result := FDataLink.Field;
end;
procedure TDBDateTimePicker.DataChange(Sender: TObject);
begin
if FDataLink.Field <> nil then
begin
if FAlignment <> FDataLink.Field.Alignment then
begin
FAlignment := FDataLink.Field.Alignment;
end;
if not (csDesigning in ComponentState) then
begin
if (FDataLink.Field.DataType in [ftString, ftWideString]) then
end;
if FFocused and FDataLink.CanModify then
Date := StrToDate(Trim(FDataLink.Field.Text))
else
begin
Date := StrToDate(FDataLink.Field.DisplayText);
end;
end else
begin
FAlignment := taLeftJustify;
if csDesigning in ComponentState then
Date := StrToDate(Trim('2000-1-1'))
end;
end;
procedure TDBDateTimePicker.EditingChange(Sender: TObject);
begin
end;
procedure TDBDateTimePicker.UpdateData(Sender: TObject);
begin
FDataLink.Modified;
FDataLink.Field.Text :=DateToStr(Date);
end;
procedure TDBDateTimePicker.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
if (Key = VK_DELETE) or ((Key = VK_INSERT) and (ssShift in Shift)) then
FDataLink.Edit;
end;
procedure TDBDateTimePicker.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if (Key in [#32..#255]) and (FDataLink.Field <> nil) and
not FDataLink.Field.IsValidChar(Key) then
begin
MessageBeep(0);
Key := #0;
end;
case Key of
^H, ^V, ^X, #32..#255:
FDataLink.Edit;
#27:
begin
FDataLink.Reset;
Key := #0;
end;
end;
end;
procedure TDBDateTimePicker.Change;
begin
inherited Change;
FDataLink.Edit;
end;
procedure Register;
begin
RegisterComponents('Swean',[TDBDateTimePicker]);
end;
end.