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

amyz發表於2007-11-16
Sender 的應用:所有Edit共用一個過濾格式 (轉)[@more@]

1. 所有的Edit 都只允許輸入數字,即只可以輸入一次小數點
2. 不想對多達幾十個的Edit 都寫onKeyPress 事件

那你就寫一個,其它的全部在 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;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752019/viewspace-982747/,如需轉載,請註明出處,否則將追究法律責任。

相關文章