嗯。。
以前贴过的
unit MyEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Menus;
type
TSetInputStyles = (stString,stInteger,stFloat,stMoney);
TSetTxtAlign=(alLeft,alRight);
type
TMyEdit = class(TEdit)
private
FInputStyle : TSetInputStyles;
FEnterTab : Boolean;
FOnKeyPress : TKeyPressEvent;
FTxtAlign:TSetTxtAlign; { Private declarations }
procedure SetInputStyle(Value : TSetInputStyles);
procedure SetEnterTab(Value : Boolean);
procedure setTxtAlign(Value:TSetTxtAlign);
protected
procedure KeyPress(var Key: Char); OverRide;
procedure CreateParams(var Params: TCreateParams); override; { Protected declarations }
public
constructor Create(AOwner : TComponent); OverRide; { Public declarations }
published
property InputStyle : TSetInputStyles Read FInputStyle Write SetInputStyle;
property EnterTab : Boolean Read FEnterTab Write SetEnterTab Default False;
property OnKeyPress : TKeyPressEvent read FOnKeyPress write FOnKeyPress;
property TxtAlign:TSetTxtAlign read FTxtAlign Write SetTxtAlign;
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('MYVCL', [TMyEdit]);
end;
{ TMyEdit }
constructor TMyEdit.Create(AOwner: TComponent);
begin
inherited;
FInputStyle := stString;
FEnterTab := False;
FTxtAlign:=alLeft;
end;
procedure TMyEdit.CreateParams(var Params: TCreateParams);
begin
inherited;
if FTxtAlign = alLeft then
Params.ExStyle:=Params.ExStyle or WS_EX_LEFT
else
Params.ExStyle:=Params.ExStyle or WS_EX_RIGHT;
end;
procedure TMyEdit.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if FEnterTab and (Key = #13) then
begin
PostMessage(Handle, WM_KEYDOWN, VK_TAB, 0);
Key := #0;
Exit;
end;
case FInputStyle of
stInteger:
if not (Key in ['0'..'9', Chr(8)]) then Key := #0;
stFloat:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
end;
stMoney:
begin
if not (Key in ['0'..'9', '.', Chr(8)]) then Key := #0;
if (Key = '.') and (Pos(Key, Text) > 0) then Key := #0;
if (Pos('.', Text) > 0) and (Key <> Chr(8)) and
((Length(Text) - Pos('.', Text)) >= 2) then Key := #0;
end;
end;
end;
procedure TMyEdit.SetEnterTab(Value: Boolean);
begin
if FEnterTab <> Value then
FEnterTab := Value;
end;
procedure TMyEdit.SetInputStyle(Value: TSetInputStyles);
begin
if Value <> FInputStyle then
FInputStyle := Value;
end;
procedure TMyEdit.setTxtAlign(Value: TSetTxtAlign);
begin
if Value <> FTxtAlign then
FTxtAlign := Value;
end;
end.