procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); var s: string; begin if Edit1.Text <> UpperCase(Edit1.Text ) then begin s := Edit1.Text ; delete(s,length(s),1); Edit1.Text := s; Edit1.SelStart := length(s); end; end;
那就判断他输入的ASCII码的范围了。
不要写在OnKeyUp里,最好用Edit1OnChange
转帖:(来自网上的技巧) 简介: 防止用户误输入是软件开发的一项必不可少的工作,除才之外,还要为用户 的使用提供最大方便。当然,我们可以利用或开发新的组件,以完成这些功能。 但是,在团队开发中,每个成员都用自己认为不错的组件开发自己所承担的模 块,会给软件的后期维护带来麻烦。交工的时候,项目负责人可不买你的帐。如 果你用函数调用来完成这些功能,老盖也管不着。下面就是针对常用delphi组件 的限制用户输入函数,但愿网友们能用的上。 (一)TEdit、TDBEdit、TComboBox、TDBComboBox的输入 分三种类型限制: (1)任意输入 (2)整数输入 (3)浮点数输入 限制的项目如下: (1)整数输入只能输入数字0-9、+、- (2)浮点输入只能输入数字0-9、+、-、. (3)+和-只能有其一,并且只能出现在最前面 (4).只能有一个 (5)限制小数位数 函数如下: procedure MxFormatKeyPress(Text:string;SelStart,SelLength:integer; var Key:Char;EditType:integer;Digits:integer); begin if (Key=#27) or (Key=#8) or (EditType=1) then exit; if EditType=2 then if not (Key in [‘0‘..‘9‘,‘+‘,‘-‘] ) then Key:=#0; if EditType=3 then if not (Key in [‘0‘..‘9‘,‘+‘,‘-‘,‘.‘] ) then Key:=#0; //控制+- if (Key =‘-‘) or (Key=‘+‘ ) then begin if ((Pos(‘-‘,Text) > 0) or (Pos(‘+‘,Text) > 0 )) and (SelLength=0 ) then Key:=#0; if SelStart > 0 then Key:=#0; end; //控制. if (Key = ‘.‘) and (EditType=3 ) then begin if (Pos(‘.‘,Text) > 0) and (not((SelStart=Pos(‘.‘,Text) ))) then Key:=#0; if SelStart=0 then Key:=#0; if (Digits>0) and (SelStart+SelLength0) and (EditType=3) then if (pos(‘.‘,Text )>0 ) and (SelStart>=pos(‘.‘,Text)) then if length(Text)-pos(‘.‘,Text )>=Digits then Key:=#0; end;此函数在所限制组件的OnKeyPress事件中调用。Key即为OnKeyPress携带的 Key:Char参数;EditType为限制的类型:1-任意输入;2-整数输入;3-浮点输入; Digits为浮点数输入时小数的位数,如果是零,则可输入任意位数。另外,此 函数只适用于有Text、SelStart、SelLength等属性的TWinControl类的派生类。 具体限制各组件的二级函数如下:限制TEdit、TDBEdit: procedure MxFormatEditKeyPress(Edit:TCustomEdit;var Key:Char;EditType:integer; Digits:integer); begin MxFormatKeyPress(Edit.Text,Edit.SelStart,Edit.SelLength,Key,EditType,Digits); end;限制TComboBox: procedure MxFormatComboKeyPress(Combo:TComboBox;var Key:Char;EditType:integer; Digits:integer); begin MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits); end;限制TDBComboBox: procedure MxFormatDBComboKeyPress(Combo:TDBComboBox;var Key:Char; EditType:integer;Digits:integer); begin MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits); end;调用示例: 假如Form1上有一ComboBox1,让用户只输入浮点数,并且小数位数为两位。则 可以在ComboBox1的OnKeyPress事件中调用上面的函数,代码如下: procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char); begin MxFormatComboKeyPress(Combobox1,Key,3,0); end;如果你的窗体上有多各TComboBox,并且限制类型一致,则不必每个TComboBox都 书写代码,只需为其中一个编写事件处理代码,其它作连接即可。 procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char); begin MxFormatComboKeyPress(Sender as TComboBox,Key,3,0); end; 其它组件调用方法同上。
仅仅是大写的话,设置CharCase为ecUpperCase就可以了
Shift: TShiftState);
var
s: string;
begin
if Edit1.Text <> UpperCase(Edit1.Text ) then
begin
s := Edit1.Text ;
delete(s,length(s),1);
Edit1.Text := s;
Edit1.SelStart := length(s);
end;
end;
简介: 防止用户误输入是软件开发的一项必不可少的工作,除才之外,还要为用户
的使用提供最大方便。当然,我们可以利用或开发新的组件,以完成这些功能。
但是,在团队开发中,每个成员都用自己认为不错的组件开发自己所承担的模
块,会给软件的后期维护带来麻烦。交工的时候,项目负责人可不买你的帐。如
果你用函数调用来完成这些功能,老盖也管不着。下面就是针对常用delphi组件
的限制用户输入函数,但愿网友们能用的上。
(一)TEdit、TDBEdit、TComboBox、TDBComboBox的输入
分三种类型限制:
(1)任意输入
(2)整数输入
(3)浮点数输入
限制的项目如下:
(1)整数输入只能输入数字0-9、+、-
(2)浮点输入只能输入数字0-9、+、-、.
(3)+和-只能有其一,并且只能出现在最前面
(4).只能有一个
(5)限制小数位数
函数如下:
procedure MxFormatKeyPress(Text:string;SelStart,SelLength:integer;
var Key:Char;EditType:integer;Digits:integer);
begin
if (Key=#27) or (Key=#8) or (EditType=1) then exit;
if EditType=2 then
if not (Key in [‘0‘..‘9‘,‘+‘,‘-‘] ) then Key:=#0;
if EditType=3 then
if not (Key in [‘0‘..‘9‘,‘+‘,‘-‘,‘.‘] ) then Key:=#0;
//控制+-
if (Key =‘-‘) or (Key=‘+‘ ) then begin
if ((Pos(‘-‘,Text) > 0) or (Pos(‘+‘,Text) > 0 )) and
(SelLength=0 ) then Key:=#0;
if SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = ‘.‘) and (EditType=3 ) then begin
if (Pos(‘.‘,Text) > 0) and (not((SelStart=Pos(‘.‘,Text) ))) then Key:=#0;
if SelStart=0 then Key:=#0;
if (Digits>0) and (SelStart+SelLength0) and (EditType=3) then
if (pos(‘.‘,Text )>0 ) and (SelStart>=pos(‘.‘,Text)) then
if length(Text)-pos(‘.‘,Text )>=Digits then Key:=#0;
end;此函数在所限制组件的OnKeyPress事件中调用。Key即为OnKeyPress携带的
Key:Char参数;EditType为限制的类型:1-任意输入;2-整数输入;3-浮点输入;
Digits为浮点数输入时小数的位数,如果是零,则可输入任意位数。另外,此
函数只适用于有Text、SelStart、SelLength等属性的TWinControl类的派生类。
具体限制各组件的二级函数如下:限制TEdit、TDBEdit:
procedure MxFormatEditKeyPress(Edit:TCustomEdit;var Key:Char;EditType:integer;
Digits:integer);
begin
MxFormatKeyPress(Edit.Text,Edit.SelStart,Edit.SelLength,Key,EditType,Digits);
end;限制TComboBox:
procedure MxFormatComboKeyPress(Combo:TComboBox;var Key:Char;EditType:integer;
Digits:integer);
begin
MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits);
end;限制TDBComboBox:
procedure MxFormatDBComboKeyPress(Combo:TDBComboBox;var Key:Char;
EditType:integer;Digits:integer);
begin
MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits);
end;调用示例:
假如Form1上有一ComboBox1,让用户只输入浮点数,并且小数位数为两位。则
可以在ComboBox1的OnKeyPress事件中调用上面的函数,代码如下:
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
MxFormatComboKeyPress(Combobox1,Key,3,0);
end;如果你的窗体上有多各TComboBox,并且限制类型一致,则不必每个TComboBox都
书写代码,只需为其中一个编写事件处理代码,其它作连接即可。
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
MxFormatComboKeyPress(Sender as TComboBox,Key,3,0);
end;
其它组件调用方法同上。