L
lzl200
Unregistered / Unconfirmed
GUEST, unregistred user!
我是一個剛學寫控件﹐下面的控件是我改寫的﹐也可以運行﹐但當改變當前日期時﹐
數據字段不自動改變﹐不知什麼地方沒有寫對﹐請幫助
unit LDate1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, DB, Mask, Menus, Extctrls, buttons ;
//uses
//Windows, Messages, SysUtils, Classes, Controls, Forms,
//Graphics, Menus, StdCtrls, ExtCtrls, Mask, Buttons, ComCtrls, Db;
type
TFiledDatalink=class(TDatalINK)
private
FField: TField;
FFieldName: string;
FControl: TComponent;
FEditing: Boolean;
FModified: Boolean;
FOnDataChange: TNotifyEvent;
FOnEditingChange: TNotifyEvent;
FOnUpdateData: TNotifyEvent;
FOnActiveChange: TNotifyEvent;
function GetCanModify: Boolean;
procedure SetEditing(Value: Boolean);
procedure SetField(Value: TField);
procedure SetFieldName(const Value: string);
procedure UpdateField;
procedure UpdateRightToLeft;
protected
procedure ActiveChanged; override;
procedure EditingChanged; override;
procedure FocusControl(Field: TFieldRef); override;
procedure LayoutChanged; override;
procedure RecordChanged(Field: TField); override;
procedure UpdateData; override;
public
constructor Create;
function Edit: Boolean;
procedure Modified;
procedure Reset;
property CanModify: Boolean read GetCanModify;
property Control: TComponent read FControl write FControl;
property Editing: Boolean read FEditing;
property Field: TField read FField;
property FieldName: string read FFieldName write SetFieldName;
property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange;
property OnEditingChange: TNotifyEvent read FOnEditingChange write FOnEditingChange;
property OnUpdateData: TNotifyEvent read FOnUpdateData write FOnUpdateData;
property OnActiveChange: TNotifyEvent read FOnActiveChange write FOnActiveChange;
end;
TListSourceLink=class(TDataLink)
public
constructor Create;
end;
TLDate1 = class(TDateTimePicker)
private
{ Private declarations }
FDataLink:TFiledDatalink;
FListLink:TListSourceLink;
function getdatasource:tdatasource;
procedure setdatasource(value:tdatasource);
function GetDataField: string;
procedure SetDataField(const Value: string);
protected
{ Protected declarations }
property listlink:tlistsourcelink read flistlink;
property datalink:tlistsourcelink read flistlink;
public
{ Public declarations }
constructor create(aowna:tcomponent);override;
published
{ Published declarations }
property DataSource:tdatasource read getdatasource write setdatasource;
property DataField: string read GetDataField write SetDataField;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TLDate1]);
end;
constructor TListSourceLink.Create;
begin
inherited Create;
VisualControl:=true;
end;
function TLDate1.getdatasource:tdatasource;
begin
result:=fdatalink.DataSource;
end;
procedure TLDate1.setdatasource(value:tdatasource);
begin
fdatalink.datasource:=value;
end;
constructor TLDate1.create;
begin
inherited create(aowna);
FDataLink :=TFiledDatalink.Create;
end;
{ TFieldDataLink }
constructor TFiledDatalink.Create;
begin
inherited Create;
VisualControl := True;
end;
procedure TFiledDatalink.SetEditing(Value: Boolean);
begin
if FEditing <> Value then
begin
FEditing := Value;
FModified := False;
if Assigned(FOnEditingChange) then FOnEditingChange(Self);
end;
end;
procedure TFiledDatalink.SetFieldName(const Value: string);
begin
if FFieldName <> Value then
begin
FFieldName := Value;
UpdateField;
end;
end;
procedure TFiledDatalink.SetField(Value: TField);
begin
if FField <> Value then
begin
FField := Value;
EditingChanged;
RecordChanged(nil);
UpdateRightToLeft;
end;
end;
procedure TFiledDatalink.UpdateField;
begin
if Active and (FFieldName <> '') then
begin
if Assigned(FControl) then
SetField(GetFieldProperty(DataSource.DataSet, FControl, FFieldName)) else
SetField(DataSource.DataSet.FieldByName(FFieldName));
end else
SetField(nil);
end;
procedure TFiledDatalink.UpdateRightToLeft;
var
IsRightAligned: Boolean;
AUseRightToLeftAlignment: Boolean;
begin
if Assigned(FControl) and (FControl is TWinControl) then
with FControl as TWinControl do
if IsRightToLeft then
begin
IsRightAligned :=
(GetWindowLong(Handle, GWL_EXSTYLE) and WS_EX_RIGHT) = WS_EX_RIGHT;
if (IsRightAligned and (not AUseRightToLeftAlignment)) or
((not IsRightAligned) and AUseRightToLeftAlignment) then
Perform(CM_RECREATEWND, 0, 0);
end;
end;
function TFiledDatalink.Edit: Boolean;
begin
if CanModify then inherited Edit;
Result := FEditing;
end;
function TFiledDatalink.GetCanModify: Boolean;
begin
Result := not ReadOnly and (Field <> nil) and Field.CanModify;
end;
procedure TFiledDatalink.Modified;
begin
FModified := True;
end;
procedure TFiledDatalink.Reset;
begin
RecordChanged(nil);
end;
procedure TFiledDatalink.ActiveChanged;
begin
UpdateField;
if Assigned(FOnActiveChange) then FOnActiveChange(Self);
end;
procedure TFiledDatalink.EditingChanged;
begin
SetEditing(inherited Editing and CanModify);
end;
procedure TFiledDatalink.FocusControl(Field: TFieldRef);
begin
if (Field^ <> nil) and (Field^ = FField) and (FControl is TWinControl) then
if TWinControl(FControl).CanFocus then
begin
Field^ := nil;
TWinControl(FControl).SetFocus;
end;
end;
procedure TFiledDatalink.RecordChanged(Field: TField);
begin
if (Field = nil) or (Field = FField) then
begin
if Assigned(FOnDataChange) then FOnDataChange(Self);
FModified := False;
end;
end;
procedure TFiledDatalink.LayoutChanged;
begin
UpdateField;
end;
procedure TFiledDatalink.UpdateData;
begin
if FModified then
begin
if (Field <> nil) and Assigned(FOnUpdateData) then FOnUpdateData(Self);
FModified := False;
end;
end;
function TLDATE1.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TLDATE1.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
end.
數據字段不自動改變﹐不知什麼地方沒有寫對﹐請幫助
unit LDate1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, DB, Mask, Menus, Extctrls, buttons ;
//uses
//Windows, Messages, SysUtils, Classes, Controls, Forms,
//Graphics, Menus, StdCtrls, ExtCtrls, Mask, Buttons, ComCtrls, Db;
type
TFiledDatalink=class(TDatalINK)
private
FField: TField;
FFieldName: string;
FControl: TComponent;
FEditing: Boolean;
FModified: Boolean;
FOnDataChange: TNotifyEvent;
FOnEditingChange: TNotifyEvent;
FOnUpdateData: TNotifyEvent;
FOnActiveChange: TNotifyEvent;
function GetCanModify: Boolean;
procedure SetEditing(Value: Boolean);
procedure SetField(Value: TField);
procedure SetFieldName(const Value: string);
procedure UpdateField;
procedure UpdateRightToLeft;
protected
procedure ActiveChanged; override;
procedure EditingChanged; override;
procedure FocusControl(Field: TFieldRef); override;
procedure LayoutChanged; override;
procedure RecordChanged(Field: TField); override;
procedure UpdateData; override;
public
constructor Create;
function Edit: Boolean;
procedure Modified;
procedure Reset;
property CanModify: Boolean read GetCanModify;
property Control: TComponent read FControl write FControl;
property Editing: Boolean read FEditing;
property Field: TField read FField;
property FieldName: string read FFieldName write SetFieldName;
property OnDataChange: TNotifyEvent read FOnDataChange write FOnDataChange;
property OnEditingChange: TNotifyEvent read FOnEditingChange write FOnEditingChange;
property OnUpdateData: TNotifyEvent read FOnUpdateData write FOnUpdateData;
property OnActiveChange: TNotifyEvent read FOnActiveChange write FOnActiveChange;
end;
TListSourceLink=class(TDataLink)
public
constructor Create;
end;
TLDate1 = class(TDateTimePicker)
private
{ Private declarations }
FDataLink:TFiledDatalink;
FListLink:TListSourceLink;
function getdatasource:tdatasource;
procedure setdatasource(value:tdatasource);
function GetDataField: string;
procedure SetDataField(const Value: string);
protected
{ Protected declarations }
property listlink:tlistsourcelink read flistlink;
property datalink:tlistsourcelink read flistlink;
public
{ Public declarations }
constructor create(aowna:tcomponent);override;
published
{ Published declarations }
property DataSource:tdatasource read getdatasource write setdatasource;
property DataField: string read GetDataField write SetDataField;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TLDate1]);
end;
constructor TListSourceLink.Create;
begin
inherited Create;
VisualControl:=true;
end;
function TLDate1.getdatasource:tdatasource;
begin
result:=fdatalink.DataSource;
end;
procedure TLDate1.setdatasource(value:tdatasource);
begin
fdatalink.datasource:=value;
end;
constructor TLDate1.create;
begin
inherited create(aowna);
FDataLink :=TFiledDatalink.Create;
end;
{ TFieldDataLink }
constructor TFiledDatalink.Create;
begin
inherited Create;
VisualControl := True;
end;
procedure TFiledDatalink.SetEditing(Value: Boolean);
begin
if FEditing <> Value then
begin
FEditing := Value;
FModified := False;
if Assigned(FOnEditingChange) then FOnEditingChange(Self);
end;
end;
procedure TFiledDatalink.SetFieldName(const Value: string);
begin
if FFieldName <> Value then
begin
FFieldName := Value;
UpdateField;
end;
end;
procedure TFiledDatalink.SetField(Value: TField);
begin
if FField <> Value then
begin
FField := Value;
EditingChanged;
RecordChanged(nil);
UpdateRightToLeft;
end;
end;
procedure TFiledDatalink.UpdateField;
begin
if Active and (FFieldName <> '') then
begin
if Assigned(FControl) then
SetField(GetFieldProperty(DataSource.DataSet, FControl, FFieldName)) else
SetField(DataSource.DataSet.FieldByName(FFieldName));
end else
SetField(nil);
end;
procedure TFiledDatalink.UpdateRightToLeft;
var
IsRightAligned: Boolean;
AUseRightToLeftAlignment: Boolean;
begin
if Assigned(FControl) and (FControl is TWinControl) then
with FControl as TWinControl do
if IsRightToLeft then
begin
IsRightAligned :=
(GetWindowLong(Handle, GWL_EXSTYLE) and WS_EX_RIGHT) = WS_EX_RIGHT;
if (IsRightAligned and (not AUseRightToLeftAlignment)) or
((not IsRightAligned) and AUseRightToLeftAlignment) then
Perform(CM_RECREATEWND, 0, 0);
end;
end;
function TFiledDatalink.Edit: Boolean;
begin
if CanModify then inherited Edit;
Result := FEditing;
end;
function TFiledDatalink.GetCanModify: Boolean;
begin
Result := not ReadOnly and (Field <> nil) and Field.CanModify;
end;
procedure TFiledDatalink.Modified;
begin
FModified := True;
end;
procedure TFiledDatalink.Reset;
begin
RecordChanged(nil);
end;
procedure TFiledDatalink.ActiveChanged;
begin
UpdateField;
if Assigned(FOnActiveChange) then FOnActiveChange(Self);
end;
procedure TFiledDatalink.EditingChanged;
begin
SetEditing(inherited Editing and CanModify);
end;
procedure TFiledDatalink.FocusControl(Field: TFieldRef);
begin
if (Field^ <> nil) and (Field^ = FField) and (FControl is TWinControl) then
if TWinControl(FControl).CanFocus then
begin
Field^ := nil;
TWinControl(FControl).SetFocus;
end;
end;
procedure TFiledDatalink.RecordChanged(Field: TField);
begin
if (Field = nil) or (Field = FField) then
begin
if Assigned(FOnDataChange) then FOnDataChange(Self);
FModified := False;
end;
end;
procedure TFiledDatalink.LayoutChanged;
begin
UpdateField;
end;
procedure TFiledDatalink.UpdateData;
begin
if FModified then
begin
if (Field <> nil) and Assigned(FOnUpdateData) then FOnUpdateData(Self);
FModified := False;
end;
end;
function TLDATE1.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;
procedure TLDATE1.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;
end.