自己写的 限制文本框TEdit中只能输入数字

我自己写的:

procedure TForm4.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0' .. '9', '.', '-', #8, #10, #13]) then
  begin
    Key := #0;
  end else begin
    if Key = '-' then
    begin
      if not string(TEdit(Sender).Text).Contains('-') then
      begin
        //光标定位在第一个上
        TEdit(Sender).SelStart := 0;
      end else begin
        Key := #0;
      end;
    end else if Key = '.' then begin
      if not string(TEdit(Sender).Text).Contains('.') then
      begin
        //光标定位在最后一个上
        TEdit(Sender).SelStart := Length(TEdit(Sender).Text);
      end else begin
        Key := #0;
      end;
    end;
  end;
end;

默认支持左右方向键

 

万一老师的博客:http://www.cnblogs.com/del/archive/2007/11/24/970893.html

 

//让 Edit 只接受数字

//方法1:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in ['0'..'9']) then
    Key := Chr(0);
end;

//方法2:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Byte(Key) in [48..57]) then  // 0的 Ascii 是 48
    Key := Chr(0);
end;

//方法3:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not CharInSet(Key, ['1'..'5']) then Key := #0;
end;

//Edit 文本的选择与光标位置
procedure TForm1.Button1Click(Sender: TObject);
begin
  Edit1.SetFocus;                           //给于焦点
  SendMessage(Edit1.Handle,EM_SETSEL,0,1);  //选择第一个字符
  SendMessage(Edit1.Handle,EM_SETSEL,0,-1); //全选
  SendMessage(Edit1.Handle,EM_SETSEL,1,1);  //光标移到第一个字符后面
  SendMessage(Edit1.Handle,EM_SETSEL,0,0);  //光标移到开始
  SendMessage(Edit1.Handle,EM_SETSEL,-1,0);  //光标移到开始
end;

//待续...

转载于:https://www.cnblogs.com/del88/p/6016818.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值