反正闲着也是闲着,顺便把这两个控件的源码贴出来。如果有朋友要急用的话,
也省的花时间写了。(第一次写控件,如有不妥之处,还请各位多多指教)。
//用于输入Currency数据的Edit控件, 添加了以下属性:
// CurrValue: 用于读取/设定Edit控件的Currency数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// NumberOfDigits: 指定小数位数.
// Prefix: 用于指定Currency的前缀(比如:$或¥).
unit CurrEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;
type
TCurrEdit = class(TCustomEdit)
private
{ Private declarations }
FCurrency: Currency;
FErrorMessage: String;
FErrorTitle: String;
FNumberOfDigits: Integer;
FPrefix: String;
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetCurrency: Currency; virtual;
procedure SetCurrency(NewCurrency: Currency); virtual;
procedure SetNumberOfDigits(NewNumberOfDigits: Integer); virtual;
procedure SetPrefix(NewPrefix: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property CurrValue: Currency read GetCurrency write SetCurrency;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property HideSelection;
property ImeMode;
property ImeName;
property NumberOfDigits: Integer read FNumberOfDigits write SetNumberOfDigits;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property Prefix: String read FPrefix write SetPrefix;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TCurrEdit]);
end;
procedure TCurrEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or ES_RIGHT;
end;
procedure TCurrEdit.DoExit;
var
TempExtended: Extended;
TempColor: TColor;
begin
inherited DoExit;
if Pos(FPrefix, Text) = 1 then
Text := Copy(Text, Length(FPrefix) + 1, Length(Text) - Length(FPrefix));
if not TextToFloat(PChar(Text), TempExtended, fvExtended) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else CurrValue := TempExtended;
end;
function TCurrEdit.GetCurrency: Currency;
begin
Result := FCurrency;
end;
procedure TCurrEdit.SetCurrency(NewCurrency: Currency);
begin
FCurrency := NewCurrency;
Text := FPrefix + CurrToStrF(FCurrency, ffNumber, FNumberOfDigits);
end;
procedure TCurrEdit.SetNumberOfDigits(NewNumberOfDigits: Integer);
begin
if NewNumberOfDigits < 0 then NewNumberOfDigits := 0;
if NewNumberOfDigits <> FNumberOfDigits then
begin
FNumberOfDigits := NewNumberOfDigits;
CurrValue := FCurrency;
end;
end;
procedure TCurrEdit.SetPrefix(NewPrefix: String);
begin
if NewPrefix <> FPrefix then
begin
FPrefix := NewPrefix;
CurrValue := FCurrency;
end;
end;
constructor TCurrEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
CurrValue := 0;
NumberOfDigits := 0;
FErrorMessage := 'Invalid Data Type.';
FErrorTitle := 'Input Error:';
FPrefix := '';
end;
destructor TCurrEdit.Destroy;
begin
inherited Destroy;
end;
end.
//用于输入Date数据的Edit控件, 添加了以下属性:
// Alignment: taLeftJustify, taRightJustify, taCenter. 指定对齐方式.
// DateValue: 用于读取/设定Edit控件的Date数据.
// ErrorMessage: 当输入错误时MessageBox的提示信息.
// ErrorTitle: 当输入错误时MessageBox的标题.
// Format: 用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
unit DateEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Graphics;
type
TDateEdit = class(TCustomEdit)
private
{ Private declarations }
FAlignment: TAlignment;
FDate: TDate;
FErrorMessage: String;
FErrorTitle: String;
FFormat: String;
//用于控制Date显示的格式,Format格式见FormatDateTime函数的帮助.
protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;
procedure DoExit; override;
function GetAlignment: TAlignment; virtual;
function GetDate: TDate; virtual;
procedure SetAlignment(NewAlignment: TAlignment); virtual;
procedure SetDate(NewDate: TDate); virtual;
procedure SetFormat(NewFormat: String); virtual;
property CharCase;
property MaxLength;
property PasswordChar;
property Text;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Alignment: TAlignment read GetAlignment write SetAlignment;
property Anchors;
property AutoSelect;
property AutoSize;
property BevelEdges;
property BevelInner;
property BevelKind default bkNone;
property BevelOuter;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property DateValue: TDate read GetDate write SetDate;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property ErrorMessage: String read FErrorMessage write FErrorMessage;
property ErrorTitle: String read FErrorTitle write FErrorTitle;
property Font;
property Format: String read FFormat write SetFormat;
property HideSelection;
property ImeMode;
property ImeName;
property OEMConvert;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnChange;
property OnClick;
property OnContextPopup;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
property OnStartDock;
property OnStartDrag;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Standard', [TDateEdit]);
end;
procedure TDateEdit.CreateParams(var Params: TCreateParams);
const
Alignments: array[TAlignment] of WORD = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style := Params.Style or Alignments[FAlignment];
end;
procedure TDateEdit.DoExit;
var
TempDateTime: TDateTime;
TempColor: TColor;
TempString: String;
Index: Integer;
begin
inherited DoExit;
TempString := Text;
Index := 1;
while (Index <= Length(TempString)) and (TempString[Index] >= '0') and
(TempString[Index] <= '9') do Inc(Index);
if Index > Length(TempString) then
begin
case Length(TempString) of
6 : TempString := Copy(TempString, 1, 2) + '-' + Copy(TempString, 3, 2) + '-' + Copy(TempString, 5, 2);
8 : TempString := Copy(TempString, 1, 4) + '-' + Copy(TempString, 5, 2) + '-' + Copy(TempString, 7, 2);
end;
end
else begin
for Index := 1 to Length(TempString) do
if (TempString[Index] < '0') or (TempString[Index] > '9') then
TempString[Index] := '-';
end;
if not TryStrToDateTime(TempString, TempDateTime) then
begin
TempColor := Font.Color;
Font.Color := clRed;
MessageBox(0, PChar(FErrorMessage), PChar(FErrorTitle), MB_ICONINFORMATION + MB_OK);
Font.Color := TempColor;
SetFocus;
end
else DateValue := TempDateTime;
end;
function TDateEdit.GetAlignment: TAlignment;
begin
GetAlignment := FAlignment;
end;
function TDateEdit.GetDate: TDate;
begin
Result := FDate;
end;
procedure TDateEdit.SetAlignment(NewAlignment: TAlignment);
begin
if FAlignment <> NewAlignment then
begin
FAlignment := NewAlignment;
if not (csLoading in componentstate) then ReCreateWnd;
end;
end;
procedure TDateEdit.SetDate(NewDate: TDate);
begin
FDate := NewDate;
Text := FormatDateTime(FFormat, FDate);
end;
procedure TDateEdit.SetFormat(NewFormat: String);
begin
if FFormat <> NewFormat then
begin
FFormat := NewFormat;
DateValue := FDate;
end;
end;
constructor TDateEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FAlignment := taLeftJustify;
DateValue := Now;
FErrorMessage := 'Invalid Date Type.';
FErrorTitle := 'Input Error:';
Format := 'yyyy''/''mm''/''dd';
end;
destructor TDateEdit.Destroy;
begin
inherited Destroy;
end;
end.