飘云阁

 找回密码
 加入我们

QQ登录

只需一步,快速开始

查看: 1887|回复: 0

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

[复制链接]
  • TA的每日心情
    开心
    2019-9-19 16:05
  • 签到天数: 4 天

    [LV.2]偶尔看看I

    发表于 2007-2-12 10:11:06 | 显示全部楼层 |阅读模式
    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;
    PYG19周年生日快乐!
    您需要登录后才可以回帖 登录 | 加入我们

    本版积分规则

    快速回复 返回顶部 返回列表