Sender 的應用:所有Edit共用一個過濾格式

王朝delphi·作者佚名  2006-01-08
宽屏版  字体: |||超大  

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;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有