如何添加TEdit仅接受数字?我搜索信息,但没有任何帮助我.
我需要一个TEdit不接受任何字母或字符串的东西.
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;
Run Code Online (Sandbox Code Playgroud)
在现代Delphi版本(D2009 +)中,您可以使用 TEdit.NumbersOnly属性.
仅允许在文本编辑中键入数字.使用NumbersOnly禁止在文本字段中输入非数字字符.但请注意,即使设置了此属性,用户也可以在文本字段中粘贴非数字字符.
另一种选择是使用TMaskEdit组件.EditMask使用以下字符的属性可以生成有效的数字输入,包括负值.
# : Accepts an optional sign or numeric digit
0 : Accepts a numeric character
9 : Accepts an optional numeric character
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14410 次 |
| 最近记录: |