TEdit,Delphi中的唯一数字 [英] Only number in TEdit, Delphi

查看:96
本文介绍了TEdit,Delphi中的唯一数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何添加仅接受数字的 TEdit
我搜索信息,但没有帮助。

How can I add a TEdit that only accept numbers? I search information but nothing helps me.

我需要一个不接受任何字母的 TEdit

I need a TEdit that does not accept any letter or strings.

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char); 
begin 
  if not (Key in [#8, '0'..'9', DecimalSeparator]) then 
  begin
     ShowMessage('Invalid key: ' + Key); 
     Key := #0; 
  end 
  else 
  if (Key = DecimalSeparator) and (Pos(Key, Edit1.Text) > 0) then 
  begin 
    ShowMessage('Invalid Key: twice ' + Key); 
    Key := #0; 
  end; 
end;


推荐答案

在现代Delphi版本(D2009 +)中,您可以使用
TEdit.NumbersOnly 属性。

In modern Delphi versions (D2009+) you can use the TEdit.NumbersOnly property.


仅允许在文本编辑中键入数字。
使用NumbersOnly禁止在文本字段中输入非数字字符。但是请注意,即使设置了此属性,用户也可以在文本字段中粘贴非数字字符。

Allows only numbers to be typed into the text edit. Use NumbersOnly to prohibit entry of non-numeric characters in the textfield. Note, however, that a user can paste non-numeric characters in the textfield even when this property is set.

另一种选择是使用 TMaskEdit 组件。
使用以下字符的 EditMask 属性可以产生有效的数字输入,包括负值。

Another option is to use the TMaskEdit component. An EditMask property using following characters can produce a valid numeric input, including negative values.

# : Accepts an optional sign or numeric digit
0 : Accepts a numeric character
9 : Accepts an optional numeric character

这篇关于TEdit,Delphi中的唯一数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆