1. 所有的Edit 都只允許輸入數字,即只可以輸入一次小數點
2. 不想對多達幾十個的Edit 都寫onKeyPress 事件
那你就寫一個,其它的全部在Object Inspector 中指向這一個:
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
IntCompIdx: Integer;
stText: string;
begin
if (Ord(Key) = 46) then
begin
IntCompIdx := (Sender as TEdit).ComponentIndex;
stText := TEdit(Components[IntCompIdx]).Text;
if Pos('.', stText) > 1 then
Key := #0;
end;
end;
如果不覺得難看的話也可以不用變量:
if (Ord(Key) = 46) and
(Pos('.', TEdit(Components[(Sender as TEdit).ComponentIndex]).Text) > 1)then
Key := #0;
博客介绍了让Edit控件只允许输入数字且仅能输入一次小数点的方法。不想为多个Edit写onKeyPress事件,可写一个后让其他Edit在Object Inspector中指向它,并给出了具体代码实现,还提供了不用变量的简化写法。
2860

被折叠的 条评论
为什么被折叠?



