如何在Edit1.OnkeyPress事件中,限制Edit1中只能输入一个 - 号,并且只能在第一位输入。

解决方案 »

  1.   

    if Edit1.Text[1] = '-' then
    ..........
      

  2.   

    楼上的说的是在onchange事件中?要求在onkeypress事件中啊。
      

  3.   

    有区别吗,不还是判断Edit1.text的值吗
      

  4.   


    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if Length(Edit1.Text)<1 then
    begin
      if Key<>'-' then
      begin
        Self.Caption:='Error';
        Key:=#0;
      end;
    end;
    end;
      

  5.   

    楼上的代码,可以在Edit中输入若干个-号。其实完整的要求是这样的,要求Edit中只能输入正常的负数和正数,而不能输入非法的负数,比如 --123 ,1--23,但是能输入像-123这样正常的负数。
      

  6.   


    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if Length(Edit1.Text)<1 then
    begin
      if Key<>'-' then
      begin
        Self.Caption:='Error';
        Key:=#0;
      end;
    end
    else if Key='-' then key:=#0;
    end;这样就只能输入一个了。
      

  7.   

    如果原来Edit中的数字是123,现在要在前面加一个负号,这个-123应该是正常的负数。如何让他能进行呢,而如果不在这个123前面的位置,则不允许输入。
      

  8.   

    这样判断如何?
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (Key in ['-','0'..'9','.',#8,#13]) then
        key:=#0
      else
        if (Key='-') and (Length(Edit1.Text)<>0) then
          key:=#0;
    end;
      

  9.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (Key in ['0'..'9', '-', #8]) then
        Key := #0
      else begin
        if Key = '-' then
        begin
          if Length(TEdit(Sender).Text) > 0 then
          begin
            if TEdit(Sender).Text[1] = '-' then
            begin
              Key := #0;
              Exit;
            end;
          end;
          if TEdit(Sender).SelStart <> 0 then
            Key := #0;
        end
        else if Key in ['0'..'9'] then
        begin
          if Length(TEdit(Sender).Text) > 0 then
          begin
            if TEdit(Sender).Text[1] = '-' then
            begin
              if TEdit(Sender).SelStart = 0 then
                Key := #0;
            end;
          end;
        end;
      end;
    end;这样应该可以了
      

  10.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Edit1.SelStart <> 0 then
      begin
        if (Key = '-') then
          Key := #0;
      end
      else
      begin
        if Pos('-', Edit1.Text) > 0 then
          Key := #0;
      end;
    end;貌似这样才是LZ想要的吧!
      

  11.   


    这个代码不错,只是当全部选中Edit中的数字,再输入数字时,输入不进去?另外,如何限制可以输入小数点,但是不能在第一位和最后一位输入小数点呢?
      

  12.   


    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Key in ['0' .. '9', '-', #8] then
      begin
        if Edit1.SelStart <> 0 then
        begin
          if (Key = '-') then
            Key := #0;
        end
        else
        begin
          if Pos('-', Edit1.Text) > 0 then
            Key := #0;
        end;
      end
      else
        Key := #0;end;
      

  13.   

    支持小数点的 这样写就行!
    判断太多了,LZ可以试着自己简化下这些判断!
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Key in ['0' .. '9', '-', '.', #8] then
      begin
        if Edit1.SelStart <> 0 then
        begin
          if (Key = '-') then
            Key := #0;
          if (Key = '.') then
          begin
            if Pos('.', Edit1.Text) > 0 then
              Key := #0;
            if Edit1.SelStart = 1 then
            begin
              if Edit1.Text[1] = '-' then
                Key := #0;
            end;
          end;
        end
        else
        begin
          if (Key = '-') then
          begin
            if Pos('-', Edit1.Text) > 0 then
              Key := #0;
          end;
          if (Key = '.') then
            Key := #0;
        end;
      end
      else
        Key := #0;end;
      

  14.   


    第一个不能输入小数点可以做到,但是最后不能输入要求在KeyPress里无法判断,除非你限制人家非要按回车标志输入结束,不然怎么知道使用者是否输入完毕呢?所以想判断是不是最后的小数点只能在提交处理的时候或者指定按回车的时候处理
      

  15.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if Length(Edit1.Text)<1 then
    begin
      if Key<>'-' then
      begin
        Self.Caption:='Error';
        Key:=#0;
      end;
    end;
    end;