请问有没有人做过Edit控件,要求只能输入整数和浮点数,急

解决方案 »

  1.   

    在Edit1的keypress事件里写
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if not (key in ['0'..'9','.',#8]) then exit;
    end;
    就可以实现你的要求了
      

  2.   

    if not (key in ['0'..'9','.',#8]) then key:=#0;
      

  3.   

    procedure TMainFrm.ByteWarnEdtKeyPress(Sender: TObject;
      var Key: Char);
    begin
      If Key in ['0'..'9',#8,'.'] then
      begin
        If Key='.' then
        begin
          With TEdit(Sender) do
          begin
            If Length(trim(Text))=0 then
              Key:=#0
            Else
            begin
              If Pos('.',Text)>0 then Key:=#0;
            end;
          end;
        end;
      end
      Else Key:=#0;
    end;
      

  4.   

    这样写可以控制只输入一个小数点
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if (not (key in ['0'..'9','.',#8])) or ((key='.') and (pos('.',Edit1.Text)>0)) then key:=#0;
    end;
      

  5.   

    在edit的onkeypress事件中写:
    if not((key in ['0'..'9',#8,'.']) ) then
        key := #0;
    就行了。
      

  6.   

    这样写可以控制只输入一个小数点
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if (not (key in ['0'..'9','.',#8])) or ((key='.') and (pos('.',Edit1.Text)>0)) then key:=#0;
    end;
      

  7.   

    請問hellolongbin #8 秋#0在這裡起什麼作用呀!
      

  8.   

    下面贴一个尚未完善的,基本上也能用。
    ==========================================
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    var
       tmpString:String;
       iSelStart:Integer;
    begin
       if Key in [#8,'1'..'9'] then exit;
       iSelStart:=TEdit(Sender).SelStart;
       case Key of
          '0':
              begin
                 tmpString:=Copy(TEdit(Sender).Text,1,iSelStart);
                 if Pos('.',tmpString)>0 then Exit;
                 case Length(tmpString) of
                    0:
                      Exit;
                    1:
                      if Pos('-',tmpString)>0 then Exit;
                 end;
                 if Length(StringReplace(StringReplace(tmpString,'0','',[rfReplaceAll]),'-','',[rfReplaceAll]))>0 then Exit;
              end;
          '.':
              if ((Pos('.',TEdit(Sender).SelText)>0) and (Pos('.',TEdit(Sender).Text)>0) or (Pos('.',TEdit(Sender).Text)=0)) then Exit;
          '-':
              if (iSelStart=0) and ((Pos('-',TEdit(Sender).SelText)>0) and (Pos('.',TEdit(Sender).Text)>0) or (Pos('-',TEdit(Sender).Text)=0)) then Exit;
       end;
       Key:=#0;
    end;
      

  9.   

    另外其实对于退格键还有一点就是串空时置'0',这个可能写到OnChange事件当中会比较合适。
      

  10.   

    立即能用
    procedure TFormAddP90TR.Edit17KeyPress(Sender: TObject; var Key: Char);
    var
      p:integer;
      tp:Tpoint;
    begin
      if key in['0'..'9','+','-','.'] then
        begin
           if key in['+','-'] then
    //************判断是否为0~9以及是否为+,-号**************
             begin
               p:=pos('+',edit17.Text)+pos('-',edit17.Text);
               if p>0 then key:=#0
               else
                 begin
                   getcaretpos(tp);
                   if tp.X>1 then key:=#0;
                 end;
             end
           else
    //*************判断是否为小数点****************
              if key='.' then
                begin
                  p:=pos('.',edit17.Text);
                  if p>0 then key:=#0;            end;
        end
       else if key>#31 then
         key:=#0;
    end;
      

  11.   

    要求结贴!如果还不喜欢楼上的说的,RAISE有个专门的NUMBER EDIT控件,满足你的要求。单我觉得楼上说的已经够了
      

  12.   

    procedure MyKeyPress(MyKess: TEdit; Len: integer; var Key: char);
    begin
       if MyKess.SelLength =length(MyKess.Text) then
          MyKess.Clear;
      if (Key <> '1') and (Key <> '2') and (Key <> '3') and (Key <> '4') and (Key <>
        '5') and (Key <> '6') and (Key <> '7') and (Key <> '8') and (Key <> '9') and
        (Key <> '0') and (Key <> '.') and (Key <> '-') then
        begin
          if Key <> #8 then
            Key := char(0);
        end
      else
        begin
          if Key = '.' then
            begin
              if pos('.', MyKess.Text) <> 0 then
                Key := char(0)
            end
          else
            if Key = '-' then
              begin
                if trim(MyKess.Text)<>'' then
                  Key := char(0)
              end
          else
            begin
              if (pos('.', MyKess.Text) <> 0) and (Length(copy(MyKess.Text, pos('.',
                MyKess.Text) + 1, Length(MyKess.Text))) > Len - 1) then
                Key := char(0)
              else
                //if Key='0' then
                  begin
                    if (trim(MyKess.text)='0') or (trim(MyKess.text)='-0') then
                      Key := char(0)
                  end;
            end;
        end;
    end;调用:
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    MyKeyPress(Edit1,2,Key);
    end;
    在onexit 事件里:procedure TForm1.Edit1Exit(Sender: TObject);
    begin
      if trim(edit1.text)='' then
        edit1.text:='0';
      if copy(trim(edit1.text),len(trim(edit1.text)),1)='.' then
        edit1.text:=copy(trim(edit1.text),1,len(trim(edit1.text))-1);
    end;
      

  13.   

    procedure MyKeyPress(MyKess: TEdit; Len: integer; var Key: char);
    begin
       if MyKess.SelLength =length(MyKess.Text) then
          MyKess.Clear;
      if (Key <> '1') and (Key <> '2') and (Key <> '3') and (Key <> '4') and (Key <>
        '5') and (Key <> '6') and (Key <> '7') and (Key <> '8') and (Key <> '9') and
        (Key <> '0') and (Key <> '.') and (Key <> '-') then
        begin
          if Key <> #8 then
            Key := char(0);
        end
      else
        begin
          if Key = '.' then
            begin
              if pos('.', MyKess.Text) <> 0 then
                Key := char(0)
            end
          else
            if Key = '-' then
              begin
                if trim(MyKess.Text)<>'' then
                  Key := char(0)
              end
          else
            begin
              if (pos('.', MyKess.Text) <> 0) and (Length(copy(MyKess.Text, pos('.',
                MyKess.Text) + 1, Length(MyKess.Text))) > Len - 1) then
                Key := char(0)
              else
                //if Key='0' then
                  begin
                    if (trim(MyKess.text)='0') or (trim(MyKess.text)='-0') then
                      Key := char(0)
                  end;
            end;
        end;
    end;调用:
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    MyKeyPress(Edit1,2,Key);
    end;
    在onexit 事件里:procedure TForm1.Edit1Exit(Sender: TObject);
    begin
      if trim(edit1.text)='' then
        edit1.text:='0';
      if copy(trim(edit1.text),len(trim(edit1.text)),1)='.' then
        edit1.text:=copy(trim(edit1.text),1,len(trim(edit1.text))-1);
    end;
      

  14.   

    网上下载这样的控件 TRzNumericEdit