打印本文 打印本文 关闭窗口 关闭窗口
Sender 的應用:所有Edit共用一個過濾格式
作者:武汉SEO闵涛  文章来源:敏韬网  点击数838  更新时间:2009/4/23 18:38:05  文章录入:mintao  责任编辑:mintao

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;

打印本文 打印本文 关闭窗口 关闭窗口