设计个从FlatSpinEdit继承的DB相关控件,不能更相关字段(100分)

  • 主题发起人 主题发起人 lewa
  • 开始时间 开始时间
L

lewa

Unregistered / Unconfirmed
GUEST, unregistred user!
初写控件,想设计个从FlatSpinEdit继承的与数据相关控件(不想从TEDit类下继承),现
在问题是可以当值变了,相关字段总不能更新,请各位高手help下。程序如下:
type
TDBFlatSpinEditFloat = class(TFlatSpinEditFloat)
private
FDataLink : TFieldDataLink;
function GetField : TField;
function GetDataField : string;
procedure SetDataField(const Value: string);
function GetDataSource : TDataSource ;
procedure SetDataSource(Value : TDataSource);
procedure CMExit(var Message:TCMExit);message CM_Exit;
procedure SetValue(Value: Extended);
protected
procedure DataChange(sender : TObject);
procedure UpdateData(sender : TObject);
procedure ActiveChange(sender : TObject);
procedure UpClick(Sender: TObject);
procedure DownClick(Sender: TObject);
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

function TDBFlatSpinEditFloat.GetDataSource: TDataSource;
begin
Result := FDataLink.DataSource;
end;

procedure TDBFlatSpinEditFloat.SetDataSource(Value: TDataSource);
begin
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
Enabled := FDataLink.Active and (FDataLink.Field <> nil) and
not FDataLink.Field.ReadOnly ;
end;

function TDBFlatSpinEditFloat.GetField: TField;
begin
Result := FDataLink.Field;
end;

function TDBFlatSpinEditFloat.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;

procedure TDBFlatSpinEditFloat.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;

constructor TDBFlatSpinEditFloat.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
//FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnActiveChange := ActiveChange;
// Enabled := false;
end;

destructor TDBFlatSpinEditFloat.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;

procedure TDBFlatSpinEditFloat.DataChange(sender : TObject);
begin
if (FDataLink.Field <> nil) and
((FDataLink.Field is TFloatField) or (FDataLink.Field is TCurrencyField)) then
value := FDataLink.Field.AsFloat
else
value := 0.00;
end;

procedure TDBFlatSpinEditFloat.UpdateData(sender : TObject);
begin
// if (FDataLink.Field <> nil) and
// ((FDataLink.Field is TFloatField) or (FDataLink.Field is TCurrencyField)) then
FDataLink.Field.AsFloat := value ;
end;

procedure TDBFlatSpinEditFloat.ActiveChange(sender : TObject);
begin
Enabled := FDataLink.Active and (FDataLink.Field <> nil);
end;

procedure TDBFlatSpinEditFloat.CMExit(var Message:TCMExit);
begin
try
FDataLink.UpdateRecord;
except
SelectAll;
SetFocus;
raise;
end;
inherited;
end;

procedure TDBFlatSpinEditFloat.SetValue(Value: Extended);
begin
FDataLink.Edit ; //使数据进入编写状态
inherited; //调用父类方法处理事件
FDataLink.Modified ;//更新了,通知系统
end;

procedure TDBFlatSpinEditFloat.UpClick(Sender: TObject);
begin
FDataLink.Edit ; //使数据进入编写状态
inherited; //调用父类方法处理事件
FDataLink.Modified ;//更新了,通知系统
end;

procedure TDBFlatSpinEditFloat.DownClick(Sender: TObject);
begin
FDataLink.Edit ; //使数据进入编写状态
inherited; //调用父类方法处理事件
FDataLink.Edit ;//更新了,通知系统
end;


procedure Register;
begin
RegisterComponents('DBFlatStyle', [TDBFlatSpinEditFloat]);
end;

end.
 
你看看你的CMExit事件中加上这两句试试?这两天不想事,没有细看.
FDataLink.Edit;
FDataLink.Modified;
改成这样.
procedure TDBFlatSpinEditFloat.CMExit(var Message:TCMExit);
begin
FDataLink.Edit;
FDataLink.Modified;
try
FDataLink.UpdateRecord;
except
SelectAll;
SetFocus;
raise;
end;
inherited;
end;

下面是一个我以前改写日历控件TDateTimePicker的DB日历控件,使用正常,你可以借鉴一下.
我没时间细看你的问题了,自己调吧.SORRY!^_^
type
TMeDBDateTimePicker = class(TDateTimePicker)
private
FDataLink: TFieldDataLink;
procedure DataChange(Sender: TObject);
procedure EditingChange(Sender: TObject);
function GetDataField: string;
function GetDataSource: TDataSource;
function GetField: TField;
function GetReadOnly: Boolean;
procedure SetDataField(const Value: string);
procedure SetDataSource(Value: TDataSource);
procedure UpdateData(Sender: TObject);
procedure CMExit(var Message: TCMExit); message CM_EXIT;
protected
procedure Change; override;
procedure Click; 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;

Procedure Register;
Begin
RegisterComponents('MySelf', [TMeDBDateTimePicker]);
end;

procedure TMeDBDateTimePicker.Change;
begin
FDataLink.Edit;
inherited Change;
FDataLink.Modified;
end;

procedure TMeDBDateTimePicker.Click;
begin
FDataLink.Edit;
inherited Click;
FDataLink.Modified;
end;

procedure TMeDBDateTimePicker.CMExit(var Message: TCMExit);
begin
FDataLink.Edit;
FDataLink.Modified;
try
FDataLink.UpdateRecord;
except
SetFocus;
raise;
end;
inherited;
end;

constructor TMeDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnEditingChange := EditingChange;
Width:=89;
end;

procedure TMeDBDateTimePicker.DataChange(Sender: TObject);
begin
if FDataLink.Field<>nil then
begin
if Length(FDataLink.Field.AsString)>0 then
DateTime:=FDataLink.Field.AsDateTime
else
begin
DateTime:=now;
FDataLink.Edit;
UpdateData(Sender);
FDataLink.Modified;
end;
end;
end;

destructor TMeDBDateTimePicker.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;

procedure TMeDBDateTimePicker.EditingChange(Sender: TObject);
begin

end;

function TMeDBDateTimePicker.GetDataField: string;
begin
Result := FDataLink.FieldName;
end;

function TMeDBDateTimePicker.GetDataSource: TDataSource;
begin
Result:=FDataLink.DataSource;
end;

function TMeDBDateTimePicker.GetField: TField;
begin
Result:= FDataLink.Field;
end;

function TMeDBDateTimePicker.GetReadOnly: Boolean;
begin

end;

procedure TMeDBDateTimePicker.SetDataField(const Value: string);
begin
FDataLink.FieldName := Value;
end;

procedure TMeDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
FDataLink.DataSource := Value;
if Value <> nil then Value.FreeNotification(Self);
end;

procedure TMeDBDateTimePicker.UpdateData(Sender: TObject);
begin
FDataLink.Field.AsDateTime:= DateTime;
end;
 
问题解决了。原来忘记写change事件
 
后退
顶部