📄 tntjvdbspinedit.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvDBSpinEdit.PAS, released on 2002-07-26.
The Initial Developer of the Original Code is Rob den Braasem []
Portions created by Rob den Braasem are Copyright (C) 2002 Rob den Braasem.
All Rights Reserved.
Contributor(s):
You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
located at http://jvcl.sourceforge.net
Known Issues:
-----------------------------------------------------------------------------}
// $Id: JvDBSpinEdit.pas,v 1.25 2005/09/26 18:58:26 ahuser Exp $
// New Jordi March: add a public property: Field
unit TntJvDBSpinEdit;
{$I jvcl.inc}
interface
uses
{$IFDEF UNITVERSIONING}
JclUnitVersioning,
{$ENDIF UNITVERSIONING}
Windows, Messages, Classes, Controls, DB, DBCtrls,
TntJvSpin, JvConsts;
type
TTntJvDBSpinEdit = class(TTntJvSpinEdit)
private
FDataLink: TFieldDataLink;
FIsNull: Boolean;
FAllowNull: Boolean;
procedure DataChange(Sender: TObject);
procedure UpdateData(Sender: TObject);
procedure EditingChange(Sender: TObject);
procedure ActiveChange(Sender: TObject);
function GetDataField: string; { Returns data field name. }
function GetDataSource: TDataSource; { Returns linked data source. }
procedure SetDataField(const NewFieldName: string); { Assigns new field. }
procedure SetDataSource(Value: TDataSource); { Assigns new data source. }
procedure CMGetDataLink(var Msg: TMessage); message CM_GETDATALINK;
function GetReadOnly: Boolean; reintroduce;
procedure SetReadOnly(Value: Boolean); reintroduce;
function GetField: TField; //New
protected
function IsValidChar(Key: Char): Boolean; override;
procedure Change; override;
procedure DoExit; override; { called to update data }
function GetValue: Extended; override;
procedure SetValue(NewValue: Extended); override;
procedure TextChanged; override;
procedure UpClick(Sender: TObject); override;
procedure DownClick(Sender: TObject); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
{ Backwards compatibility; eventually remove }
procedure DefineProperties(Filer: TFiler); override;
procedure ReadReadOnlyField(Reader: TReader);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property IsNull: Boolean read FIsNull;
property Field: TField read GetField; //New
published
property AllowNull: Boolean read FAllowNull write FAllowNull default True;
property DataField: string read GetDataField write SetDataField;
property DataSource: TDataSource read GetDataSource write SetDataSource;
property ReadOnly: Boolean read GetReadOnly write SetReadOnly default False;
end;
{$IFDEF UNITVERSIONING}
const
UnitVersioning: TUnitVersionInfo = (
RCSfile: '$RCSfile: JvDBSpinEdit.pas,v $';
Revision: '$Revision: 1.25 $';
Date: '$Date: 2005/09/26 18:58:26 $';
LogPath: 'JVCL'run'
);
{$ENDIF UNITVERSIONING}
implementation
uses
{$IFDEF HAS_UNIT_VARIANTS}
Variants,
{$ENDIF HAS_UNIT_VARIANTS}
SysUtils;
//=== { TTntJvDBSpinEdit } ======================================================
constructor TTntJvDBSpinEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
inherited ReadOnly := True;
FAllowNull := True;
FDataLink := TFieldDataLink.Create;
FDataLink.Control := Self;
FDataLink.OnDataChange := DataChange;
FDataLink.OnEditingChange := EditingChange;
FDataLink.OnUpdateData := UpdateData;
FDataLink.OnActiveChange := ActiveChange;
end;
destructor TTntJvDBSpinEdit.Destroy;
begin
FDataLink.Free;
FDataLink := nil;
inherited Destroy;
end;
procedure TTntJvDBSpinEdit.ActiveChange(Sender: TObject);
begin
if not (csDesigning in ComponentState) then
Enabled := FDataLink.Active;
end;
procedure TTntJvDBSpinEdit.Change;
begin
if (FDataLink <> nil) and (FDataLink.Field <> nil) then
FDataLink.Modified;
inherited Change;
end;
procedure TTntJvDBSpinEdit.CMGetDataLink(var Msg: TMessage);
begin
Msg.Result := Longint(FDataLink);
end;
procedure TTntJvDBSpinEdit.DataChange(Sender: TObject); { Triggered when data changes in DataSource. }
begin
if FDataLink.Field <> nil then
begin
if Focused and FDataLink.CanModify then
Text := FDataLink.Field.Text
else
begin
FIsNull := (FDataLink.Field.DisplayText = '');
Text := FDataLink.Field.DisplayText;
if FDataLink.Editing or (FDataLink.Field.DataSet.State = dsInsert) then
Modified := True;
end;
end
else
begin
FIsNull := False;
if csDesigning in ComponentState then
Text := Name
else
Text := '';
end;
end;
procedure TTntJvDBSpinEdit.DefineProperties(Filer: TFiler);
begin
inherited DefineProperties(Filer);
Filer.DefineProperty('ReadOnlyField', ReadReadOnlyField, nil, False);
end;
procedure TTntJvDBSpinEdit.DoExit;
begin
try
if Modified then
FDataLink.Modified;
FDataLink.UpdateRecord; { tell data link to update database }
except
SetFocus; { if it failed, don't let focus leave }
SelectAll;
raise;
end;
inherited DoExit;
end;
procedure TTntJvDBSpinEdit.DownClick(Sender: TObject);
begin
FDataLink.Edit;
if IsNull then
begin
FIsNull := False;
Value := 1;
FIsNull := False;
Text := '1';
end;
inherited DownClick(Sender);
end;
procedure TTntJvDBSpinEdit.EditingChange(Sender: TObject);
begin
inherited ReadOnly := not FDataLink.Editing;
end;
function TTntJvDBSpinEdit.GetDataField: string; { Returns data field name. }
begin
{ FDataLink is built in TTntJvDBSpinEdit.Create; there's no need to check to see if it's assigned. }
Result := FDataLink.FieldName;
end;
function TTntJvDBSpinEdit.GetDataSource: TDataSource; { Returns linked data source. }
begin
{ FDataLink is built in TTntJvDBSpinEdit.Create; there's no need to check to see if it's assigned. }
Result := FDataLink.DataSource;
end;
function TTntJvDBSpinEdit.GetReadOnly: Boolean;
begin
Result := FDataLink.ReadOnly;
end;
function TTntJvDBSpinEdit.GetValue: Extended;
begin
Result := inherited GetValue;
FIsNull := (Text = '') and (Result = 0.0);
end;
function TTntJvDBSpinEdit.IsValidChar(Key: Char): Boolean;
begin
Result := inherited IsValidChar(Key);
if not Result and AllowNull and
(Key = BackSpace) or (Key = Del) then
Result := True;
end;
procedure TTntJvDBSpinEdit.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited KeyDown(Key, Shift);
if (Key = VK_DELETE) or (Key = VK_BACK) or
((Key = VK_INSERT) and (ssShift in Shift)) or IsValidChar(Char(Key)) then
FDataLink.Edit;
end;
procedure TTntJvDBSpinEdit.ReadReadOnlyField(Reader: TReader);
begin
ReadOnly := Reader.ReadBoolean;
end;
procedure TTntJvDBSpinEdit.SetDataField(const NewFieldName: string); { Assigns new field. }
begin
{ FDataLink is built in TTntJvDBSpinEdit.Create; there's no need to check to see if it's assigned. }
FDataLink.FieldName := NewFieldName;
end;
procedure TTntJvDBSpinEdit.SetDataSource(Value: TDataSource); { Assigns new data source. }
begin
{ FDataLink is built in TTntJvDBSpinEdit.Create; there's no need to check to see if it's assigned. }
if not (FDataLink.DataSourceFixed and (csLoading in ComponentState)) then
FDataLink.DataSource := Value;
{ Tell the new DataSource that our TTntJvDBSpinEdit component should be notified
(using the Notification method) if the DataSource is ever removed from
a data module or form that is different than the owner of this control. }
if Value <> nil then
Value.FreeNotification(Self);
//FDataLink.DataSource := NewSource;
//if NewSource <> nil then
// NewSource.FreeNotification(Self);
end;
procedure TTntJvDBSpinEdit.SetReadOnly(Value: Boolean);
begin
FDataLink.ReadOnly := Value;
end;
procedure TTntJvDBSpinEdit.SetValue(NewValue: Extended);
begin
FIsNull := (Text = '') and (NewValue = 0.0);
inherited SetValue(NewValue);
end;
procedure TTntJvDBSpinEdit.TextChanged;
begin
if FIsNull and AllowNull then
inherited Text := ''
else
inherited TextChanged;
end;
procedure TTntJvDBSpinEdit.UpClick(Sender: TObject);
begin
FDataLink.Edit;
if IsNull then
begin
FIsNull := False;
Value := 1;
FIsNull := False;
Text := '1';
end
else
inherited UpClick(Sender);
end;
{ UpdateData is only called after calls to both FDataLink.Modified and
FDataLink.UpdateRecord. }
procedure TTntJvDBSpinEdit.UpdateData(Sender: TObject);
begin
{ Never masked? }
{ValidateEdit;}
if FDataLink.Editing then
FDataLink.Field.Text := Text;
end;
function TTntJvDBSpinEdit.GetField: TField; {New}
begin
Result := FDataLink.Field;
end;
{$IFDEF UNITVERSIONING}
initialization
RegisterUnitVersion(HInstance, UnitVersioning);
finalization
UnregisterUnitVersion(HInstance);
{$ENDIF UNITVERSIONING}
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -