两天前写了一个组件,主要是为了过滤数字的。
现在帖出来。
没有仔细验证,大家帮忙看看。
{*******************************************************}
{ }
{ 编写的第五个组件 }
{ }
{ 此组件增加Edit的过滤功能 }
{ 过滤选择在属性的FilterOption中设置 }
{ }
{ 影子,2002年1月28日 }
{ Email:shadow@cncorn.com }
{ }
{ 1月29日新增 }
{ 增加MaxValue,MinValue设置 }
{ GetLastErr为最后的警告信息,可以在OnKeyUp中提示警告 }
{ 增加了对齐方式Alignment }
{ }
{*******************************************************}
unit FilterEdit;
interface
uses
Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Clipbrd;
type
TFilterOption = (foNormal,foNumber,foLetter,foReal); //正常,数字,字母,实数
TMinRange = 0..9; //最小范围,不能超过9
type
TFilterEdit = class(TEdit)
private
FFilterOption: TFilterOption;
{如果FilterOption<>foNumber,或者FMinValue>=FMaxValue,FMinValue,FMaxValue均无效。}
FMinValue: TMinRange; //允许的最小值,不能超过9
FMaxValue: DWORD; //允许的最大值
FAlignment: TAlignment; //对齐方式
function CheckOption(str: string;foOption: TFilterOption):boolean; //检测哪个选项,并执行相应代码
procedure SetAlignment(Value: TAlignment);
procedure CreateParams(var Params: TCreateParams); override;
protected
procedure KeyPress(var Key: Char); override;
procedure SetFilterOption(Value: TFilterOption);
procedure SetMinValue(Value: TMinRange);
procedure SetMaxValue(Value: DWORD);
procedure WMPaste(var Message: TWMPaste);message WM_PASTE;
public
GetLastErr: string; //错误信息
constructor Create(AOwner: TComponent);override;
procedure CreateWnd;override;
published
property FilterOption : TFilterOption read FFilterOption write SetFilterOption default foNormal;
property MinValue : TMinRange read FMinValue write SetMinValue default 0;
property MaxValue : DWORD read FMaxValue write SetMaxValue default 0;
property Alignment: TAlignment read FAlignment write SetAlignment;
end;
procedure Register;
implementation
function TFilterEdit.CheckOption(str: string;foOption: TFilterOption):boolean;
var
i : integer;
textTmp : string; //临时保存Text的内容
begin
result := true;
case foOption of
foNormal:
begin
//
end;
foNumber:
begin
if FMinValue >= FMaxValue then
begin
for i := 1 to length(str) do
begin
if (str>'9') or (str<'0') then
begin
GetLastErr := '输入的不是数字';
result := false;
break;
end;
end;
end
else
begin
if (str[1]>'9') or (str[1]<'0') or (strtoint(str[1])>MaxValue) then //是否数字并且输入是否大于最大值
begin
GetLastErr := '输入的不是数字或者超出'+inttostr(MaxValue);
result := false;
exit;
end;
textTmp := Text;
delete(textTmp,SelStart+1,SelLength);
//将str 插入SelStart位置
textTmp := copy(textTmp,1,SelStart)+str+copy(textTmp,SelStart+1,length(textTmp)-SelStart);
if (strtoint(textTmp[1])=0) and (MinValue>0) then //最小值不为0,但第一位是0
begin
GetLastErr := '第一位不允许为0';
result := false;
exit;
end;
if (strtoint64(textTmp)>MaxValue) or (strtoint64(textTmp)<MinValue) then
begin
GetLastErr := '超出范围,注意范围为:'+inttostr(MinValue)+' 到 '+inttostr(MaxValue);
result := false;
exit;
end;
end;
end;
foLetter:
begin
for i := 1 to length(str) do
begin
if (UpCase(str)<'A') or (UpCase(str)>'Z') then
begin
GetLastErr := '输入的不是字母';
result := false;
break;
end;
end;
end;
foReal:
begin
for i := 1 to length(str) do
begin
if (str<>'.') and ((str>'9') or (str<'0')) then
begin
GetLastErr := '请注意什么是实数';
result := false;
exit;
end;
end;
textTmp := Text;
delete(textTmp,SelStart+1,SelLength);
//将str 插入SelStart位置
textTmp := copy(textTmp,1,SelStart)+str+copy(textTmp,SelStart+1,length(textTmp)-SelStart);
if (length(textTmp)>1) and (textTmp[1]='0') and (textTmp[2]<>'.') then
begin
GetLastErr := '难道要我告诉你实数的格式吗?';
result := false;
exit;
end;
i := pos('.',textTmp);
if i<>0 then
begin
delete(textTmp,1,i);
if pos('.',textTmp)<>0 then
begin
GetLastErr := '实数会有两个小数点吗?';
result := false;
exit;
end;
end;
end;
end; //end case
end;
procedure TFilterEdit.CreateParams(var Params: TCreateParams);
begin
inherited CreateParams(Params);
case FAlignment of
taLeftJustify:
begin
Params.Style := Params.Style + ES_LEFT;
end;
taRightJustify:
begin
Params.Style := Params.Style + ES_RIGHT;
end;
taCenter:
begin
Params.Style := Params.Style + ES_CENTER;
end;
end;
end;
procedure TFilterEdit.SetAlignment(Value: TAlignment);
begin
if FAlignment<>Value then
begin
FAlignment := Value;
RecreateWnd;
end;
end;
constructor TFilterEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
procedure TFilterEdit.CreateWnd;
begin
inherited CreateWnd;
if FFilterOption<>foNormal then
if not CheckOption(text,FFilterOption) then
text := '';
GetLastErr := '';
end;
procedure TFilterEdit.KeyPress(var Key: Char);
begin
if not(Key in [#8,#9,#13,#27]) then //除BackSpace,Tab,Enter,Esc键外
begin
if not(CheckOption(key,FFilterOption)) and not((GetKeyState(VK_CONTROL) and $80)<>0) then
begin
key := #0;
end;
end
else
inherited; //Enable Delete
//else if (key=chr(22){字母V,字母A为1,B为2,为什么呢?}) and ((GetKeyState(VK_CONTROL) and $80)<>0) and ClipBoard.HasFormat(CF_TEXT) then
{ begin
if not CheckOption(ClipBoard.AsText,FFilterOption) then
begin
key := #0;
end
end
else
key := #0;
注释代码不能接收鼠标PASTE消息,故不要
}
end;
procedure TFilterEdit.SetFilterOption(Value: TFilterOption);
begin
if Value <> FFilterOption then
begin
FFilterOption := Value;
Invalidate;
end;
end;
procedure TFilterEdit.SetMinValue(Value: TMinRange);
begin
if Value<>FMinValue then
begin
FMinValue := Value;
Invalidate;
end;
end;
procedure TFilterEdit.SetMaxValue(Value: DWORD);
begin
if Value<>FMaxValue then
begin
FMaxValue := Value;
Invalidate;
end;
end;
procedure TFilterEdit.WMPaste(var Message: TWMPaste);//message WM_PASTE
begin
if ClipBoard.HasFormat(CF_TEXT) then
begin
if CheckOption(ClipBoard.AsText,FFilterOption) then
begin
inherited;
end;
end;
end;
procedure Register;
begin
RegisterComponents('dwh', [TFilterEdit]);
end;
end.