key 限制字符的输入

//限制字符的输入
{ 只能输入以下字符 }
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  If (Key in ['\', '/', ':', '*', '?', '<', '>', '|']) then
    Key := #0;
  If not(Key in ['0' .. '9', 'a' .. 'z', 'A' .. 'Z']) then
    Key := #0;
end;


只能数字且只能输入一个小数点
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key in ['0' .. '9', '.', #8]) then
    Key := #0;
  if (Key = '.') and (Pos('.', Edit1.Text) > 0) then
    Key := #0; // 只能数字且只能输入一个小数点
end;
  
//只允许输入数字的TEdit组件
procedure TForm1.FormCreate(Sender: TObject);
var
    wl:Integer;
begin
    wl:=GetWindowLong(Edit1.Handle, GWL_STYLE);
    SetWindowLong(Edit1.Handle, GWL_STYLE, wl or ES_NUMBER);
end;

//Delphi XE4
Edit1.NumbersOnly:=True;

















//让edit 或memo右键无效措施:
//1. 设置edit或memo的ContextPopup事件 设置: Handled:=True; 即使右键对当前控件无效
//2.可以使用一个的空的PopupMenu1 这样可以视为右键无效
//右键粘贴无效
procedure TForm1.Edit1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin
    Handled:=True;
end;
 




posted @ 2014-07-29 18:42  XE2011  阅读(144)  评论(0编辑  收藏  举报