TEdit,Delphi中的唯一数字 [英] Only number in 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屋!