procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  If Key=8 Then                         //允许退格键
    Key:=8
  Else If Key=46 Then                   //如果输入小数点(ascii=46)
  begin
    If usdPoint=False Then              //如果以前未输入过小数点
    begin
      Key:=46;
      usdPoint:=True;                   //将小数点输入标志改为true
    end
    Else
      Key:=0;                           //如果以前输入过则不实行任何操作
  end
  Else if (Key<48) Or (Key>57) Then     //输入非数字则不实行任何操作
    Key:=0;
end;
我不知道错在哪里?我想实现:在edit中按下键时判断是否为数字或小数点及退格键,如果不是就不显示。

解决方案 »

  1.   

    if Key not in [48..57,46,8] then Key := 0;
      

  2.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      If Key=#8 Then                         //允许退格键
        Key:=#8
      Else If Key=#46 Then                   //如果输入小数点(ascii=46)
      begin
        If usdPoint=False Then              //如果以前未输入过小数点
        begin
          Key:=#46;
          usdPoint:=True;                   //将小数点输入标志改为true
        end
        Else
          Key:=#0;                           //如果以前输入过则不实行任何操作
      end
      Else if (Key<#48) Or (Key>#57) Then     //输入非数字则不实行任何操作
        Key:=#0;
    end;你试一下。
      

  3.   

    procedure TValueInfoForm.edtValueKeyPress(Sender: TObject; var Key: Char);
    begin
    //  inherited;  if key in ['0'..'9', '.'] then
      begin
        if (key = '.') and (pos('.', TEdit(Sender).Text) > 0) then key:=#0;
      end
      else if key > #31 then key := #0;  
    end;
      

  4.   

    定义单元变量:
    var
      inputpoint :boolean;
    初始化为true;begin
      if (key in ['.']) then inputpoint = false;
      if not (key in ['0'..'9']) then begin
        if ((key in ['.']) and inputpoint = false) then key := #0 ;
        key := #0;
      end;
    end;
      

  5.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if key not in ['0'..'9', '.',#8] then
         key:=#0;
    end;