文本框只能输入数字

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


在ONKEYPRESS里写下
procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if key>'9' or key<'0' then abort
end;


procedure TSellForm.NumEditKeyPress(Sender: TObject; var Key: Char);
begin
  if key in ['0'..'9'] then
  else
    Key:=#0;
end;


procedure TQueryBilForm.ComPTextKeyPress(Sender: TObject; var Key: Char);
begin
  if Assigned(CurField)  then
   begin
    if (CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
      begin
      if not((key in ['0'..'9','.','-',#8])) then
       begin
        key := #0
       end
      else
        begin
          if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
             Key:=#0
          else if
             (Key='-') and (Pos('-',TEdit(Sender).Text)<>0) then
             Key:=#0;
        end;
      end
    else if (CurField.DataType in [ftAutoInc,ftSmallint, ftInteger, ftWord,
                 ftBytes,ftLargeint]) then
       begin
       if not ((Key in ['0'..'9','-',#8])) then
        begin
         Key:=#0
        end
        else
        begin
          if(Key='-') and (Pos('-',TEdit(Sender).Text)<>0) then
             Key:=#0;
        end;
       end
    else
       Exit;
   end;
end;

 

try
  StrtoFloat(Edit1.Text);
except
  messagebox(handle,'请输入数字!',mb_ok);
end;

 

Edit 中只输入数字
    SetWindowLong(Edit1.Handle, GWL_STYLE,
                  GetWindowLong(Edit1.Handle, GWL_STYLE) or
                  ES_NUMBER); 

在edit1 keypress事件中加入以下代码即可:

procedure TForm1.edit1KeyPress(Sender: TObject; var Key: Char);
begin
if not (key in ['0'..'9','.',#13,#8]) then  key:=#0;
if (key = '.') and (Pos('.', edit1.text) > 0) then   key:= #0;
end;

 

if Not(Key in ['0'..'9'])
再加两个,
if Not(Key in ['0'..'9',#8,#13])
不然按删除或者都会出提示

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if Not(Key in ['0'..'9',#8,#13]) then
begin
key:=#0; //把输入的字符清空
ShowMessage('输入不是数字');
end;
end;

 

Var
I : Integer;
Begin
Try
I := StrToInt(Edit1.Text);
Except
ShowMessage('XXXX');
end;

 

[分享]限制文本框只能输入数字(delphi)
if not (key in ['.','0'..'9',#8,#13])then key:=#0;               //只能输入数字、小数点和回车、退格
if (key in ['.'])and (pos('.',Edit1.Text)>0)then key:=#0;     //只能输入一个小数点
if (key in ['.'])and(length(Edit1.Text)<1)then key:=#0;       //第一位不能为小数点
if (key in ['0'])and (pos('.',Edit1.Text)<1)and(copy(Edit1.Text,1,1)='0')   then
key:=#0;

//------------------------------------------------------------------------------------------
//只可输入数值
if key=#8 then
  else if (key<#48) or (key>#57) then
    begin
      application.MessageBox('您输入的不是数值!','SUER个人工作室',mb_ok+mb_iconquestion);
      key:=#0;
    end
else
//------------------------------------------------------------------------------------------

 

你可能感兴趣的:(只能输入数字)