我自己的函数,在TEdit的OnKeyPress事件中写
另外你参考一下这个贴子
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1385827
procedure CheckKey(Sender: TObject; Index: integer; var Key: Char);
var
Found: boolean;
i,SelSt,j: Integer;
TmpStr: string;
begin
with (Sender as TEdit) do
begin
j :=0;
SelSt :=SelStart;
if (Key = Chr(vk_Back)) and (SelLength <> 0) then
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelLength+SelStart+1,255)
else if Key = Chr(vk_Back) then {SelLength = 0}
TmpStr := Copy(Text,1,SelStart-1)+Copy(Text,SelStart+1,255)
else if Key in ['0'..'9'] then
begin
TmpStr := Copy(Text,1,SelStart)+Key+Copy(Text,SelLength+SelStart+1,255);
j :=1;
end
else {Key in ['A'..'Z', etc]}
TmpStr := Copy(Text,1,SelStart)+Copy(Text,SelStart+1,255);
if (Key = Chr(vk_Back)) and (SelSt > 0) then
Dec(SelSt)
else if Key <> Chr(vk_Back) then
Inc(SelSt);
Key := #0; { indicate that key was handled }
if SelSt = 0 then
begin
Text:= '';
Exit;
end
else
begin
Text :=TmpStr;
SelStart :=SelSt+j;
end;
end;
end;