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;
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;这样就只能输入一个了。
这样判断如何? 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;
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;这样应该可以了
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想要的吧!
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;
支持小数点的 这样写就行! 判断太多了,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;
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;
..........
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;
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;这样就只能输入一个了。
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;
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;这样应该可以了
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想要的吧!
这个代码不错,只是当全部选中Edit中的数字,再输入数字时,输入不进去?另外,如何限制可以输入小数点,但是不能在第一位和最后一位输入小数点呢?
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;
判断太多了,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;
第一个不能输入小数点可以做到,但是最后不能输入要求在KeyPress里无法判断,除非你限制人家非要按回车标志输入结束,不然怎么知道使用者是否输入完毕呢?所以想判断是不是最后的小数点只能在提交处理的时候或者指定按回车的时候处理
begin
if Length(Edit1.Text)<1 then
begin
if Key<>'-' then
begin
Self.Caption:='Error';
Key:=#0;
end;
end;
end;