我想可以在输入时检查一下,这是在OnKeyPress事件里的代码:
procedure TForm1.DBEdit1KeyPress(Sender: TObject; var Key: Char);
begin
  try
    if (StrToInt(DBEditDay.Text + Key) > 255) or (StrToInt(DBEditDay.Text + Key) < -254)then
      Key := #0;
  except
  end;
end;

解决方案 »

  1.   

    对不起,应该是:
    if (StrToInt(DBEditDay.Text + Key) > 32767) or (StrToInt(DBEditDay.Text + Key) < -32768) then
          
      

  2.   

    在 yourfield 的 OnSetText Event中拦截三少 :o)
      

  3.   

    同ePing,在TFILED的onsettext事件里检查输入值,再自己更新数据。onvalidate事件也可以检查有效性,但要和editmask属性合用。建议这种方法。详细可见
    DELPHI的帮助。
      

  4.   

    //我试了一下, TField 里的事件都不能做这些。
    //所以我在DBGrid1.KeyDown中打主意。请看:
    function TForm1.GetDBGridEditingText(ADBGrid: TDBGrid): string;
    var
      i: integer;
    begin
      Result := '';
      for i := 0 to ADBGrid.ControlCount - 1 do
        if (ADBGrid.Controls[i] is TCustomEdit) and (TCustomEdit(ADBGrid.Controls[i]).Focused) then
          Result := TCustomEdit(ADBGrid.Controls[i]).Text;
    end;//判断它输入后,是否大于33000,小于33000,
    //而不是在beforePost,afterpost...
    procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    var Value:string;
    begin
      case dbgrid1.selectedindex of
        0://field1(smallint)
        begin
          Value:=GetDBGridEditingText(DBGrid1);
          if Value<>'' then
            if ((strtoint(Value)>=33000) or
             (strtoint(Value)<=-33000)) then
            begin
              Showmessage('不能输入大于33000,小于33000的数');
              Abort;//
            end;
        end;
      end;
    end;
      

  5.   

    saoren, 你的方法也可以, 不过TField里也行, 只不过如果你在onsettext事件中有处理的
    话,就要自己负责更新数据,可看Tfield源码,他跳过默认操作了。
    procedure TField.SetEditText(const Value: string);
    begin
      if Assigned(FOnSetText) then FOnSetText(Self, Value) else SetText(Value);
         ^^^^^^^^^^^^^^^^^^^                                     ^^^^^默认的操作
    end;
      

  6.   

    cow
    根据tina的要求,field1>32675,<-32675,
    在DBGrid中你只要打了回车,OnSetEditText事件没触发,倒是先抛出异常Eng
    所以我觉得还是在onkeydown中处理好点。
      

  7.   

    怎么会呢? 
    如果在ONKEYDOWN处理,用户用鼠标也粘贴数据的话就无能为力了。
    当然应该在ONSETTEXT里处理,不知你说的ONSETEDITTEXT事件是那个控件的?